|
| 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/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 9 | + "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 11 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional" |
| 12 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account" |
| 13 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/types" |
| 14 | +) |
| 15 | + |
| 16 | +func DataSourceCockpitSource() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + ReadContext: dataSourceCockpitSourceRead, |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "id": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Optional: true, |
| 23 | + Computed: true, |
| 24 | + Description: "ID of the data source.", |
| 25 | + }, |
| 26 | + "region": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Computed: true, |
| 29 | + Description: "The region of the data source.", |
| 30 | + }, |
| 31 | + "project_id": account.ProjectIDSchema(), |
| 32 | + "name": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Optional: true, |
| 35 | + Computed: true, |
| 36 | + Description: "The name of the data source.", |
| 37 | + }, |
| 38 | + "type": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + Computed: true, |
| 42 | + Description: "The type of the data source (e.g., 'metrics', 'logs', 'traces').", |
| 43 | + ValidateFunc: validation.StringInSlice([]string{ |
| 44 | + "metrics", "logs", "traces", |
| 45 | + }, false), |
| 46 | + }, |
| 47 | + "origin": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Optional: true, |
| 50 | + Computed: true, |
| 51 | + Description: "The origin of the data source (e.g., 'scaleway', 'external', 'custom').", |
| 52 | + ValidateFunc: validation.StringInSlice([]string{ |
| 53 | + "scaleway", "external", "custom", |
| 54 | + }, false), |
| 55 | + }, |
| 56 | + "url": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + Description: "The URL of the data source.", |
| 60 | + }, |
| 61 | + "created_at": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + Description: "The creation date of the data source.", |
| 65 | + }, |
| 66 | + "updated_at": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "The last update date of the data source.", |
| 70 | + }, |
| 71 | + "synchronized_with_grafana": { |
| 72 | + Type: schema.TypeBool, |
| 73 | + Computed: true, |
| 74 | + Description: "Whether the data source is synchronized with Grafana.", |
| 75 | + }, |
| 76 | + "retention_days": { |
| 77 | + Type: schema.TypeInt, |
| 78 | + Computed: true, |
| 79 | + Description: "The retention period of the data source in days.", |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func dataSourceCockpitSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 86 | + if _, ok := d.GetOk("id"); ok { |
| 87 | + return fetchDataSourceByID(ctx, d, meta) |
| 88 | + } |
| 89 | + return fetchDataSourceByFilters(ctx, d, meta) |
| 90 | +} |
| 91 | + |
| 92 | +func fetchDataSourceByID(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 93 | + regionalID := d.Get("id").(string) |
| 94 | + api, region, id, err := NewAPIWithRegionAndID(meta, regionalID) |
| 95 | + if err != nil { |
| 96 | + return diag.FromErr(err) |
| 97 | + } |
| 98 | + d.SetId(id) |
| 99 | + res, err := api.GetDataSource(&cockpit.RegionalAPIGetDataSourceRequest{ |
| 100 | + Region: region, |
| 101 | + DataSourceID: id, |
| 102 | + }, scw.WithContext(ctx)) |
| 103 | + if err != nil { |
| 104 | + return diag.FromErr(err) |
| 105 | + } |
| 106 | + flattenDataSource(d, res) |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func fetchDataSourceByFilters(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 111 | + api, region, err := cockpitAPIWithRegion(d, meta) |
| 112 | + if err != nil { |
| 113 | + return diag.FromErr(err) |
| 114 | + } |
| 115 | + |
| 116 | + req := &cockpit.RegionalAPIListDataSourcesRequest{ |
| 117 | + Region: region, |
| 118 | + ProjectID: d.Get("project_id").(string), |
| 119 | + } |
| 120 | + |
| 121 | + if v, ok := d.GetOk("type"); ok { |
| 122 | + req.Types = []cockpit.DataSourceType{cockpit.DataSourceType(v.(string))} |
| 123 | + } |
| 124 | + if v, ok := d.GetOk("origin"); ok { |
| 125 | + req.Origin = cockpit.DataSourceOrigin(v.(string)) |
| 126 | + } |
| 127 | + |
| 128 | + res, err := api.ListDataSources(req, scw.WithContext(ctx), scw.WithAllPages()) |
| 129 | + if err != nil { |
| 130 | + return diag.FromErr(err) |
| 131 | + } |
| 132 | + |
| 133 | + if res.TotalCount == 0 { |
| 134 | + return diag.Errorf("no data source found matching the specified criteria") |
| 135 | + } |
| 136 | + |
| 137 | + if name, ok := d.GetOk("name"); ok { |
| 138 | + for _, ds := range res.DataSources { |
| 139 | + if ds.Name == name.(string) { |
| 140 | + flattenDataSource(d, ds) |
| 141 | + return nil |
| 142 | + } |
| 143 | + } |
| 144 | + return diag.Errorf("no data source found with name '%s'", name.(string)) |
| 145 | + } |
| 146 | + |
| 147 | + flattenDataSource(d, res.DataSources[0]) |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +func flattenDataSource(d *schema.ResourceData, ds *cockpit.DataSource) { |
| 152 | + d.SetId(regional.NewIDString(ds.Region, ds.ID)) |
| 153 | + _ = d.Set("project_id", ds.ProjectID) |
| 154 | + _ = d.Set("name", ds.Name) |
| 155 | + _ = d.Set("url", ds.URL) |
| 156 | + _ = d.Set("type", ds.Type.String()) |
| 157 | + _ = d.Set("origin", ds.Origin.String()) |
| 158 | + _ = d.Set("created_at", types.FlattenTime(ds.CreatedAt)) |
| 159 | + _ = d.Set("updated_at", types.FlattenTime(ds.UpdatedAt)) |
| 160 | + _ = d.Set("synchronized_with_grafana", ds.SynchronizedWithGrafana) |
| 161 | + _ = d.Set("retention_days", int(ds.RetentionDays)) |
| 162 | + _ = d.Set("region", ds.Region.String()) |
| 163 | +} |
0 commit comments