diff --git a/docs/resources/ai_provider.md b/docs/resources/ai_provider.md index 99f62cd..78d6e28 100644 --- a/docs/resources/ai_provider.md +++ b/docs/resources/ai_provider.md @@ -81,6 +81,7 @@ resource "coderd_ai_provider" "openai" { type = "openai" name = "openai" display_name = "OpenAI" + icon = "/icon/openai.svg" enabled = true base_url = "https://api.openai.com/v1" @@ -106,6 +107,7 @@ resource "coderd_ai_provider" "openai" { - `api_key_wo_version` (Number) Version for the write-only API key. Required when `api_key_wo` is set; bump it whenever `api_key_wo` changes to rotate the stored key. - `display_name` (String) Display name shown in Coder. If omitted, defaults to the provider name. - `enabled` (Boolean) Whether this AI provider is enabled. Defaults to true. +- `icon` (String) Relative path or external URL that specifies an icon to be displayed in the dashboard. If omitted, Coder shows the default icon for the provider type. Requires Coder v2.36.0 or later. - `settings` (Attributes) Type-specific provider settings. Removing them from an existing provider replaces the provider. (see [below for nested schema](#nestedatt--settings)) ### Read-Only diff --git a/examples/resources/coderd_ai_provider/resource.tf b/examples/resources/coderd_ai_provider/resource.tf index ceb9978..a14b621 100644 --- a/examples/resources/coderd_ai_provider/resource.tf +++ b/examples/resources/coderd_ai_provider/resource.tf @@ -54,6 +54,7 @@ resource "coderd_ai_provider" "openai" { type = "openai" name = "openai" display_name = "OpenAI" + icon = "/icon/openai.svg" enabled = true base_url = "https://api.openai.com/v1" diff --git a/internal/provider/ai_provider_resource.go b/internal/provider/ai_provider_resource.go index bf83996..a0b49a6 100644 --- a/internal/provider/ai_provider_resource.go +++ b/internal/provider/ai_provider_resource.go @@ -18,13 +18,17 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-log/tflog" ) -const aiProviderClaudePlatformAWSMinVersion = "2.38.0" +const ( + aiProviderClaudePlatformAWSMinVersion = "2.38.0" + aiProviderIconMinVersion = "2.36.0" +) var ( bedrockCanonicalBaseURLRegex = regexp.MustCompile(`(?i)^https://bedrock-runtime\.([a-z0-9-]+)\.amazonaws\.com/?$`) @@ -50,6 +54,7 @@ type AIProviderResourceModel struct { Type types.String `tfsdk:"type"` Name types.String `tfsdk:"name"` DisplayName types.String `tfsdk:"display_name"` + Icon types.String `tfsdk:"icon"` Enabled types.Bool `tfsdk:"enabled"` BaseURL types.String `tfsdk:"base_url"` APIKeyWO types.String `tfsdk:"api_key_wo"` @@ -175,6 +180,12 @@ func (r *AIProviderResource) Schema(ctx context.Context, req resource.SchemaRequ stringplanmodifier.UseStateForUnknown(), }, }, + "icon": schema.StringAttribute{ + MarkdownDescription: "Relative path or external URL that specifies an icon to be displayed in the dashboard. If omitted, Coder shows the default icon for the provider type. Requires Coder v" + aiProviderIconMinVersion + " or later.", + Optional: true, + Computed: true, + Default: stringdefault.StaticString(""), + }, "enabled": schema.BoolAttribute{ MarkdownDescription: "Whether this AI provider is enabled. Defaults to true.", Optional: true, @@ -497,6 +508,7 @@ func (r *AIProviderResource) Create(ctx context.Context, req resource.CreateRequ } checkBedrockRoleARNDropped(config, provider, &resp.Diagnostics) checkBedrockProtocolDropped(config, provider, &resp.Diagnostics) + checkIconDropped(plan, provider, &resp.Diagnostics) if resp.Diagnostics.HasError() { return } @@ -573,6 +585,7 @@ func (r *AIProviderResource) Update(ctx context.Context, req resource.UpdateRequ } checkBedrockRoleARNDropped(config, provider, &resp.Diagnostics) checkBedrockProtocolDropped(config, provider, &resp.Diagnostics) + checkIconDropped(plan, provider, &resp.Diagnostics) if resp.Diagnostics.HasError() { return } @@ -614,6 +627,7 @@ func (m AIProviderResourceModel) createRequest(config AIProviderResourceModel, d Type: codersdk.AIProviderType(m.Type.ValueString()), Name: m.Name.ValueString(), DisplayName: config.DisplayName.ValueString(), + Icon: m.Icon.ValueString(), Enabled: m.Enabled.ValueBool(), BaseURL: m.BaseURL.ValueString(), APIKeys: apiKeys, @@ -627,6 +641,10 @@ func (m AIProviderResourceModel) updateRequest(state, config AIProviderResourceM v := config.DisplayName.ValueString() patch.DisplayName = &v } + if !m.Icon.Equal(state.Icon) { + v := m.Icon.ValueString() + patch.Icon = &v + } if !m.Enabled.Equal(state.Enabled) { v := m.Enabled.ValueBool() patch.Enabled = &v @@ -731,6 +749,7 @@ func (m AIProviderResourceModel) stateFromProvider(provider codersdk.AIProvider) Type: types.StringValue(string(provider.Type)), Name: types.StringValue(provider.Name), DisplayName: types.StringValue(provider.DisplayName), + Icon: types.StringValue(provider.Icon), Enabled: types.BoolValue(provider.Enabled), BaseURL: types.StringValue(provider.BaseURL), CreatedAt: types.Int64Value(provider.CreatedAt.Unix()), @@ -830,6 +849,20 @@ func checkBedrockProtocolDropped(config AIProviderResourceModel, provider coders ) } +// A Coder server older than v2.36.0 drops the unknown icon JSON key, so a +// configured value round-trips to empty and Terraform reports an inconsistent +// result. Fail loudly instead (mirrors checkBedrockRoleARNDropped). +func checkIconDropped(plan AIProviderResourceModel, provider codersdk.AIProvider, diags *diag.Diagnostics) { + if plan.Icon.IsNull() || plan.Icon.IsUnknown() || plan.Icon.ValueString() == "" || provider.Icon != "" { + return + } + diags.AddAttributeError( + path.Root("icon"), + "AI provider icon not supported by this Coder deployment", + "The Coder server accepted the request but did not persist `icon`, which means it predates AI provider icons. Upgrade to Coder v"+aiProviderIconMinVersion+" or later, or remove `icon`.", + ) +} + // bedrockProtocol converts a framework protocol value into its SDK form, // mapping null/unknown to the zero value (which the server resolves to // invoke-model). diff --git a/internal/provider/ai_provider_resource_test.go b/internal/provider/ai_provider_resource_test.go index 84e6234..dfb67cc 100644 --- a/internal/provider/ai_provider_resource_test.go +++ b/internal/provider/ai_provider_resource_test.go @@ -49,17 +49,21 @@ func TestAccAIProviderResource(t *testing.T) { Token: client.SessionToken(), OpenAIKey: "sk-test-primary-000000", OpenAIKeyVersion: 1, + OpenAIIcon: "/icon/openai.svg", BedrockBaseURL: "https://bedrock-runtime.us-east-1.amazonaws.com", } cfg2 := cfg1 cfg2.OpenAIKey = "sk-test-primary-111111" cfg2.OpenAIKeyVersion = 2 + cfg2.OpenAIIcon = "/emojis/1f916.png" // Changing base_url to a different region must re-derive settings.bedrock.region. cfg3 := cfg2 cfg3.BedrockBaseURL = "https://bedrock-runtime.us-west-2.amazonaws.com" cfg4 := cfg3 cfg4.OpenAIKey = "" cfg4.OpenAIKeyVersion = 3 + // Removing icon from configuration clears it. + cfg4.OpenAIIcon = "" resource.Test(t, resource.TestCase{ IsUnitTest: true, @@ -71,6 +75,8 @@ func TestAccAIProviderResource(t *testing.T) { Config: cfg1.String(t), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("coderd_ai_provider.openai", "name", "openai-acc"), + resource.TestCheckResourceAttr("coderd_ai_provider.openai", "icon", cfg1.OpenAIIcon), + resource.TestCheckResourceAttr("coderd_ai_provider.bedrock", "icon", ""), resource.TestCheckResourceAttr("coderd_ai_provider.openai", "api_key_masked", aibridgeutils.MaskSecret(cfg1.OpenAIKey)), resource.TestCheckNoResourceAttr("coderd_ai_provider.openai", "api_key_wo"), resource.TestCheckResourceAttr("coderd_ai_provider.bedrock", "settings.bedrock.region", "us-east-1"), @@ -88,6 +94,7 @@ func TestAccAIProviderResource(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("coderd_ai_provider.openai", "api_key_wo_version", "2"), resource.TestCheckResourceAttr("coderd_ai_provider.openai", "api_key_masked", aibridgeutils.MaskSecret(cfg2.OpenAIKey)), + resource.TestCheckResourceAttr("coderd_ai_provider.openai", "icon", cfg2.OpenAIIcon), ), }, { @@ -98,7 +105,10 @@ func TestAccAIProviderResource(t *testing.T) { }, { Config: cfg4.String(t), - Check: resource.TestCheckNoResourceAttr("coderd_ai_provider.openai", "api_key_masked"), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckNoResourceAttr("coderd_ai_provider.openai", "api_key_masked"), + resource.TestCheckResourceAttr("coderd_ai_provider.openai", "icon", ""), + ), }, }, }) @@ -109,6 +119,7 @@ type testAccAIProviderResourceConfig struct { Token string OpenAIKey string OpenAIKeyVersion int + OpenAIIcon string BedrockBaseURL string } @@ -124,6 +135,9 @@ resource "coderd_ai_provider" "openai" { type = "openai" name = "openai-acc" display_name = "OpenAI Acceptance" +{{- if .OpenAIIcon}} + icon = "{{.OpenAIIcon}}" +{{- end}} enabled = true base_url = "https://api.openai.com/v1" @@ -1209,6 +1223,34 @@ func TestAIProviderStateFromProviderMapsEmptyBedrockStringsToNull(t *testing.T) require.True(t, b.RoleARN.IsNull()) } +func TestAIProviderCheckIconDropped(t *testing.T) { + t.Parallel() + + const icon = "/icon/openai.svg" + for name, tc := range map[string]struct { + plan types.String + response string + wantError bool + }{ + "icon stripped by old server": {plan: types.StringValue(icon), response: "", wantError: true}, + "icon persisted": {plan: types.StringValue(icon), response: icon}, + "icon not configured": {plan: types.StringValue(""), response: ""}, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + + var diags diag.Diagnostics + checkIconDropped(AIProviderResourceModel{Icon: tc.plan}, codersdk.AIProvider{Icon: tc.response}, &diags) + if tc.wantError { + require.True(t, diags.HasError(), "expected a diagnostic") + require.Contains(t, diags.Errors()[0].Summary(), "AI provider icon not supported by this Coder deployment") + } else { + require.False(t, diags.HasError(), diags.Errors()) + } + }) + } +} + func TestAIProviderCheckBedrockRoleARNDropped(t *testing.T) { t.Parallel() @@ -1400,6 +1442,38 @@ func TestAIProviderCreateRequestClaudePlatformAWS(t *testing.T) { }, req.Settings.ClaudePlatformAWS) } +func TestAIProviderUpdateRequestIcon(t *testing.T) { + t.Parallel() + + for name, tc := range map[string]struct { + state, plan string + want *string + }{ + "unchanged": {state: "/icon/openai.svg", plan: "/icon/openai.svg", want: nil}, + "changed": {state: "/icon/openai.svg", plan: "/emojis/1f916.png", want: new("/emojis/1f916.png")}, + "cleared": {state: "/icon/openai.svg", plan: "", want: new("")}, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + + state := AIProviderResourceModel{ + Type: types.StringValue(string(codersdk.AIProviderTypeOpenAI)), + DisplayName: types.StringValue("OpenAI"), + Icon: types.StringValue(tc.state), + Enabled: types.BoolValue(true), + BaseURL: types.StringValue("https://api.openai.com/v1"), + } + plan := state + plan.Icon = types.StringValue(tc.plan) + + var diags diag.Diagnostics + patch := plan.updateRequest(state, plan, &diags) + require.False(t, diags.HasError(), diags.Errors()) + require.Equal(t, tc.want, patch.Icon) + }) + } +} + func TestAIProviderUpdateRequestClaudePlatformAWS(t *testing.T) { t.Parallel()