|
| 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 | + Description: "The origin of the data source (e.g., 'scaleway', 'external', 'custom').", |
| 51 | + Computed: true, |
| 52 | + ValidateFunc: validation.StringInSlice([]string{ |
| 53 | + "scaleway", "external", "custom", |
| 54 | + }, false), |
| 55 | + }, |
| 56 | + // Computed fields |
| 57 | + "url": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + Description: "The URL of the data source.", |
| 61 | + }, |
| 62 | + "created_at": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + Description: "The creation date of the data source.", |
| 66 | + }, |
| 67 | + "updated_at": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + Description: "The last update date of the data source.", |
| 71 | + }, |
| 72 | + "synchronized_with_grafana": { |
| 73 | + Type: schema.TypeBool, |
| 74 | + Computed: true, |
| 75 | + Description: "Whether the data source is synchronized with Grafana.", |
| 76 | + }, |
| 77 | + "retention_days": { |
| 78 | + Type: schema.TypeInt, |
| 79 | + Computed: true, |
| 80 | + Description: "The retention period of the data source in days.", |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func dataSourceCockpitSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 87 | + |
| 88 | + if _, ok := d.GetOk("id"); ok { |
| 89 | + return fetchDataSourceByID(ctx, d, meta) |
| 90 | + } |
| 91 | + |
| 92 | + return fetchDataSourceByFilters(ctx, d, meta) |
| 93 | +} |
| 94 | + |
| 95 | +func fetchDataSourceByID(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 96 | + |
| 97 | + regionalID := d.Get("id").(string) |
| 98 | + api, region, id, err := NewAPIWithRegionAndID(meta, regionalID) |
| 99 | + d.SetId(id) |
| 100 | + if err != nil { |
| 101 | + return diag.FromErr(err) |
| 102 | + } |
| 103 | + res, err := api.GetDataSource(&cockpit.RegionalAPIGetDataSourceRequest{ |
| 104 | + Region: region, |
| 105 | + DataSourceID: id, |
| 106 | + }, scw.WithContext(ctx)) |
| 107 | + if err != nil { |
| 108 | + return diag.FromErr(err) |
| 109 | + } |
| 110 | + flattenDataSource(d, res) |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func fetchDataSourceByFilters(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 115 | + |
| 116 | + api, region, err := cockpitAPIWithRegion(d, meta) |
| 117 | + if err != nil { |
| 118 | + return diag.FromErr(err) |
| 119 | + } |
| 120 | + |
| 121 | + req := &cockpit.RegionalAPIListDataSourcesRequest{ |
| 122 | + Region: region, |
| 123 | + ProjectID: d.Get("project_id").(string), |
| 124 | + } |
| 125 | + |
| 126 | + if v, ok := d.GetOk("type"); ok { |
| 127 | + req.Types = []cockpit.DataSourceType{cockpit.DataSourceType(v.(string))} |
| 128 | + } |
| 129 | + if v, ok := d.GetOk("origin"); ok { |
| 130 | + req.Origin = cockpit.DataSourceOrigin(v.(string)) |
| 131 | + } |
| 132 | + var allDataSources []*cockpit.DataSource |
| 133 | + page := int32(1) |
| 134 | + for { |
| 135 | + req.Page = &page |
| 136 | + req.PageSize = scw.Uint32Ptr(1000) |
| 137 | + |
| 138 | + res, err := api.ListDataSources(req, scw.WithContext(ctx)) |
| 139 | + if err != nil { |
| 140 | + return diag.FromErr(err) |
| 141 | + } |
| 142 | + |
| 143 | + allDataSources = append(allDataSources, res.DataSources...) |
| 144 | + |
| 145 | + if len(res.DataSources) < 1000 { |
| 146 | + break |
| 147 | + } |
| 148 | + |
| 149 | + page++ |
| 150 | + } |
| 151 | + |
| 152 | + if len(allDataSources) == 0 { |
| 153 | + return diag.Errorf("no data source found matching the specified criteria") |
| 154 | + } |
| 155 | + |
| 156 | + if name, ok := d.GetOk("name"); ok { |
| 157 | + for _, ds := range allDataSources { |
| 158 | + if ds.Name == name.(string) { |
| 159 | + flattenDataSource(d, ds) |
| 160 | + return nil |
| 161 | + } |
| 162 | + } |
| 163 | + return diag.Errorf("no data source found with name '%s'", name.(string)) |
| 164 | + } |
| 165 | + |
| 166 | + flattenDataSource(d, allDataSources[0]) |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +func flattenDataSource(d *schema.ResourceData, ds *cockpit.DataSource) { |
| 171 | + d.SetId(regional.NewIDString(ds.Region, ds.ID)) |
| 172 | + _ = d.Set("project_id", ds.ProjectID) |
| 173 | + _ = d.Set("name", ds.Name) |
| 174 | + _ = d.Set("url", ds.URL) |
| 175 | + _ = d.Set("type", ds.Type.String()) |
| 176 | + _ = d.Set("origin", ds.Origin.String()) |
| 177 | + _ = d.Set("created_at", types.FlattenTime(ds.CreatedAt)) |
| 178 | + _ = d.Set("updated_at", types.FlattenTime(ds.UpdatedAt)) |
| 179 | + _ = d.Set("synchronized_with_grafana", ds.SynchronizedWithGrafana) |
| 180 | + _ = d.Set("retention_days", int(ds.RetentionDays)) |
| 181 | + _ = d.Set("region", ds.Region.String()) |
| 182 | +} |
0 commit comments