|
| 1 | +package scaleway |
| 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/schema" |
| 9 | + tem "github.com/scaleway/scaleway-sdk-go/api/tem/v1alpha1" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 11 | +) |
| 12 | + |
| 13 | +func dataSourceScalewayTemDomain() *schema.Resource { |
| 14 | + // Generate datasource schema from resource |
| 15 | + dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayTemDomain().Schema) |
| 16 | + |
| 17 | + // Set 'Optional' schema elements |
| 18 | + addOptionalFieldsToSchema(dsSchema, "name", "region") |
| 19 | + |
| 20 | + dsSchema["name"].ConflictsWith = []string{"domain_id"} |
| 21 | + dsSchema["domain_id"] = &schema.Schema{ |
| 22 | + Type: schema.TypeString, |
| 23 | + Optional: true, |
| 24 | + Description: "The ID of the tem domain", |
| 25 | + ValidateFunc: validationUUIDorUUIDWithLocality(), |
| 26 | + ConflictsWith: []string{"name"}, |
| 27 | + } |
| 28 | + |
| 29 | + return &schema.Resource{ |
| 30 | + ReadContext: dataSourceScalewayTemDomainRead, |
| 31 | + |
| 32 | + Schema: dsSchema, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func dataSourceScalewayTemDomainRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 37 | + api, region, err := temAPIWithRegion(d, meta) |
| 38 | + if err != nil { |
| 39 | + return diag.FromErr(err) |
| 40 | + } |
| 41 | + |
| 42 | + domainID, ok := d.GetOk("domain_id") |
| 43 | + if !ok { |
| 44 | + res, err := api.ListDomains(&tem.ListDomainsRequest{ |
| 45 | + Region: region, |
| 46 | + Name: expandStringPtr(d.Get("name")), |
| 47 | + ProjectID: expandStringPtr(d.Get("project_id")), |
| 48 | + }, scw.WithContext(ctx)) |
| 49 | + if err != nil { |
| 50 | + return diag.FromErr(err) |
| 51 | + } |
| 52 | + |
| 53 | + for _, domain := range res.Domains { |
| 54 | + if domain.Status == tem.DomainStatusRevoked { |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + if domain.Name == d.Get("name").(string) { |
| 59 | + if domainID != "" { |
| 60 | + return diag.FromErr(fmt.Errorf("more than 1 server found with the same name %s", d.Get("name"))) |
| 61 | + } |
| 62 | + |
| 63 | + domainID = domain.ID |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if domainID == "" { |
| 68 | + return diag.FromErr(fmt.Errorf("no domain found with the name %s", d.Get("name"))) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + regionalID := datasourceNewRegionalizedID(domainID, region) |
| 73 | + d.SetId(regionalID) |
| 74 | + err = d.Set("domain_id", regionalID) |
| 75 | + if err != nil { |
| 76 | + return diag.FromErr(err) |
| 77 | + } |
| 78 | + |
| 79 | + diags := resourceScalewayTemDomainRead(ctx, d, meta) |
| 80 | + if diags != nil { |
| 81 | + return append(diags, diag.Errorf("failed to read tem domain state")...) |
| 82 | + } |
| 83 | + |
| 84 | + if d.Id() == "" { |
| 85 | + return diag.Errorf("tem domain (%s) not found", regionalID) |
| 86 | + } |
| 87 | + |
| 88 | + return nil |
| 89 | +} |
0 commit comments