|
| 1 | +package cockpit |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1" |
| 9 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 10 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource" |
| 11 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional" |
| 12 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/meta" |
| 13 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/types" |
| 14 | +) |
| 15 | + |
| 16 | +func DataSourceCockpitSources() *schema.Resource { |
| 17 | + dsSchema := datasource.SchemaFromResourceSchema(ResourceCockpitSource().Schema) |
| 18 | + |
| 19 | + sourceElementSchema := datasource.SchemaFromResourceSchema(ResourceCockpitSource().Schema) |
| 20 | + delete(sourceElementSchema, "sources") |
| 21 | + |
| 22 | + sourceElementSchema["id"] = &schema.Schema{ |
| 23 | + Type: schema.TypeString, |
| 24 | + Description: "The ID of the data source.", |
| 25 | + Computed: true, |
| 26 | + } |
| 27 | + |
| 28 | + dsSchema["sources"] = &schema.Schema{ |
| 29 | + Type: schema.TypeList, |
| 30 | + Description: "List of cockpit sources.", |
| 31 | + Computed: true, |
| 32 | + Elem: &schema.Resource{ |
| 33 | + Schema: sourceElementSchema, |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + datasource.FixDatasourceSchemaFlags(dsSchema, false, "name", "project_id") |
| 38 | + datasource.AddOptionalFieldsToSchema(dsSchema, "region", "type", "origin") |
| 39 | + |
| 40 | + return &schema.Resource{ |
| 41 | + ReadContext: dataSourceCockpitSourcesRead, |
| 42 | + Schema: dsSchema, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func dataSourceCockpitSourcesRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { |
| 47 | + var region scw.Region |
| 48 | + var err error |
| 49 | + |
| 50 | + if v, ok := d.GetOk("region"); ok && v.(string) != "" { |
| 51 | + region, err = scw.ParseRegion(v.(string)) |
| 52 | + if err != nil { |
| 53 | + return diag.FromErr(err) |
| 54 | + } |
| 55 | + } else { |
| 56 | + _, region, err = cockpitAPIWithRegion(d, m) |
| 57 | + if err != nil { |
| 58 | + return diag.FromErr(err) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + api := cockpit.NewRegionalAPI(meta.ExtractScwClient(m)) |
| 63 | + |
| 64 | + req := &cockpit.RegionalAPIListDataSourcesRequest{ |
| 65 | + Region: region, |
| 66 | + ProjectID: d.Get("project_id").(string), |
| 67 | + } |
| 68 | + |
| 69 | + if v, ok := d.GetOk("type"); ok { |
| 70 | + req.Types = []cockpit.DataSourceType{cockpit.DataSourceType(v.(string))} |
| 71 | + } |
| 72 | + |
| 73 | + if v, ok := d.GetOk("origin"); ok { |
| 74 | + req.Origin = cockpit.DataSourceOrigin(v.(string)) |
| 75 | + } |
| 76 | + |
| 77 | + res, err := api.ListDataSources(req, scw.WithContext(ctx), scw.WithAllPages()) |
| 78 | + if err != nil { |
| 79 | + return diag.FromErr(err) |
| 80 | + } |
| 81 | + |
| 82 | + sources := []any(nil) |
| 83 | + |
| 84 | + for _, ds := range res.DataSources { |
| 85 | + if name, ok := d.GetOk("name"); ok { |
| 86 | + if ds.Name != name.(string) { |
| 87 | + continue |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + rawSource := flattenDataSourceToMap(ds) |
| 92 | + sources = append(sources, rawSource) |
| 93 | + } |
| 94 | + |
| 95 | + d.SetId(region.String()) |
| 96 | + _ = d.Set("sources", sources) |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +func flattenDataSourceToMap(ds *cockpit.DataSource) map[string]any { |
| 102 | + pushURL, _ := createCockpitPushURL(ds.Type, ds.URL) |
| 103 | + |
| 104 | + return map[string]any{ |
| 105 | + "id": regional.NewIDString(ds.Region, ds.ID), |
| 106 | + "project_id": ds.ProjectID, |
| 107 | + "name": ds.Name, |
| 108 | + "url": ds.URL, |
| 109 | + "type": ds.Type.String(), |
| 110 | + "origin": ds.Origin.String(), |
| 111 | + "created_at": types.FlattenTime(ds.CreatedAt), |
| 112 | + "updated_at": types.FlattenTime(ds.UpdatedAt), |
| 113 | + "synchronized_with_grafana": ds.SynchronizedWithGrafana, |
| 114 | + "retention_days": int(ds.RetentionDays), |
| 115 | + "region": ds.Region.String(), |
| 116 | + "push_url": pushURL, |
| 117 | + } |
| 118 | +} |
0 commit comments