|
| 1 | +package asserts |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + "gopkg.in/yaml.v2" |
| 11 | + |
| 12 | + assertsapi "github.com/grafana/grafana-asserts-public-clients/go/gcom" |
| 13 | + "github.com/grafana/terraform-provider-grafana/v4/internal/common" |
| 14 | +) |
| 15 | + |
| 16 | +func makeResourceLogDrilldownConfig() *common.Resource { |
| 17 | + sch := &schema.Resource{ |
| 18 | + Description: "Manages Asserts Log Drilldown Environment Configuration through the Grafana API.", |
| 19 | + |
| 20 | + CreateContext: resourceLogDrilldownConfigCreate, |
| 21 | + ReadContext: resourceLogDrilldownConfigRead, |
| 22 | + UpdateContext: resourceLogDrilldownConfigUpdate, |
| 23 | + DeleteContext: resourceLogDrilldownConfigDelete, |
| 24 | + |
| 25 | + Importer: &schema.ResourceImporter{StateContext: schema.ImportStatePassthroughContext}, |
| 26 | + |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "name": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + ForceNew: true, |
| 32 | + Description: "The environment name for the log drilldown configuration.", |
| 33 | + }, |
| 34 | + "config": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + Description: "The environment configuration (EnvironmentDto), in YAML format.", |
| 38 | + }, |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + return common.NewLegacySDKResource( |
| 43 | + common.CategoryAsserts, |
| 44 | + "grafana_asserts_log_drilldown_config", |
| 45 | + common.NewResourceID(common.StringIDField("name")), |
| 46 | + sch, |
| 47 | + ) |
| 48 | +} |
| 49 | + |
| 50 | +func resourceLogDrilldownConfigCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 51 | + client, stackID, diags := validateAssertsClient(meta) |
| 52 | + if diags.HasError() { |
| 53 | + return diags |
| 54 | + } |
| 55 | + |
| 56 | + name := d.Get("name").(string) |
| 57 | + cfgYAML := d.Get("config").(string) |
| 58 | + |
| 59 | + var env assertsapi.EnvironmentDto |
| 60 | + if err := yaml.Unmarshal([]byte(cfgYAML), &env); err != nil { |
| 61 | + return diag.FromErr(fmt.Errorf("failed to unmarshal environment YAML: %w", err)) |
| 62 | + } |
| 63 | + // Ensure name is set from resource name |
| 64 | + env.SetName(name) |
| 65 | + |
| 66 | + req := client.LogConfigControllerAPI.UpsertEnvironmentConfig(ctx). |
| 67 | + EnvironmentDto(env). |
| 68 | + XScopeOrgID(fmt.Sprintf("%d", stackID)) |
| 69 | + _, err := req.Execute() |
| 70 | + if err != nil { |
| 71 | + return diag.FromErr(fmt.Errorf("failed to upsert log environment config: %w", err)) |
| 72 | + } |
| 73 | + |
| 74 | + d.SetId(name) |
| 75 | + return resourceLogDrilldownConfigRead(ctx, d, meta) |
| 76 | +} |
| 77 | + |
| 78 | +func resourceLogDrilldownConfigRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 79 | + client, stackID, diags := validateAssertsClient(meta) |
| 80 | + if diags.HasError() { |
| 81 | + return diags |
| 82 | + } |
| 83 | + name := d.Id() |
| 84 | + |
| 85 | + var tenantCfg *assertsapi.TenantEnvConfigResponseDto |
| 86 | + err := withRetryRead(ctx, func(retryCount, maxRetries int) *retry.RetryError { |
| 87 | + resp, _, err := client.LogConfigControllerAPI.GetTenantEnvConfig(ctx). |
| 88 | + XScopeOrgID(fmt.Sprintf("%d", stackID)). |
| 89 | + Execute() |
| 90 | + if err != nil { |
| 91 | + return createAPIError("get tenant environment log config", retryCount, maxRetries, err) |
| 92 | + } |
| 93 | + tenantCfg = resp |
| 94 | + return nil |
| 95 | + }) |
| 96 | + if err != nil { |
| 97 | + return diag.FromErr(err) |
| 98 | + } |
| 99 | + |
| 100 | + if tenantCfg == nil { |
| 101 | + d.SetId("") |
| 102 | + return nil |
| 103 | + } |
| 104 | + |
| 105 | + found := false |
| 106 | + for _, env := range tenantCfg.GetEnvironments() { |
| 107 | + if env.GetName() == name { |
| 108 | + found = true |
| 109 | + break |
| 110 | + } |
| 111 | + } |
| 112 | + if !found { |
| 113 | + d.SetId("") |
| 114 | + return nil |
| 115 | + } |
| 116 | + |
| 117 | + _ = d.Set("name", name) |
| 118 | + if v := d.Get("config").(string); v != "" { |
| 119 | + _ = d.Set("config", v) |
| 120 | + } |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func resourceLogDrilldownConfigUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 125 | + client, stackID, diags := validateAssertsClient(meta) |
| 126 | + if diags.HasError() { |
| 127 | + return diags |
| 128 | + } |
| 129 | + |
| 130 | + name := d.Get("name").(string) |
| 131 | + cfgYAML := d.Get("config").(string) |
| 132 | + |
| 133 | + var env assertsapi.EnvironmentDto |
| 134 | + if err := yaml.Unmarshal([]byte(cfgYAML), &env); err != nil { |
| 135 | + return diag.FromErr(fmt.Errorf("failed to unmarshal environment YAML: %w", err)) |
| 136 | + } |
| 137 | + env.SetName(name) |
| 138 | + |
| 139 | + req := client.LogConfigControllerAPI.UpsertEnvironmentConfig(ctx). |
| 140 | + EnvironmentDto(env). |
| 141 | + XScopeOrgID(fmt.Sprintf("%d", stackID)) |
| 142 | + _, err := req.Execute() |
| 143 | + if err != nil { |
| 144 | + return diag.FromErr(fmt.Errorf("failed to upsert log environment config: %w", err)) |
| 145 | + } |
| 146 | + |
| 147 | + return resourceLogDrilldownConfigRead(ctx, d, meta) |
| 148 | +} |
| 149 | + |
| 150 | +func resourceLogDrilldownConfigDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 151 | + client, stackID, diags := validateAssertsClient(meta) |
| 152 | + if diags.HasError() { |
| 153 | + return diags |
| 154 | + } |
| 155 | + name := d.Id() |
| 156 | + |
| 157 | + req := client.LogConfigControllerAPI.DeleteConfig(ctx, name). |
| 158 | + XScopeOrgID(fmt.Sprintf("%d", stackID)) |
| 159 | + _, err := req.Execute() |
| 160 | + if err != nil { |
| 161 | + return diag.FromErr(fmt.Errorf("failed to delete log environment config: %w", err)) |
| 162 | + } |
| 163 | + return nil |
| 164 | +} |
0 commit comments