|
| 1 | +package scaleway |
| 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 | + webhosting "github.com/scaleway/scaleway-sdk-go/api/webhosting/v1alpha1" |
| 9 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 10 | +) |
| 11 | + |
| 12 | +func dataSourceScalewayWebhosting() *schema.Resource { |
| 13 | + dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayWebhosting().Schema) |
| 14 | + |
| 15 | + addOptionalFieldsToSchema(dsSchema, "domain") |
| 16 | + |
| 17 | + dsSchema["domain"].ConflictsWith = []string{"webhosting_id"} |
| 18 | + dsSchema["webhosting_id"] = &schema.Schema{ |
| 19 | + Type: schema.TypeString, |
| 20 | + Optional: true, |
| 21 | + Description: "The ID of the Webhosting", |
| 22 | + ValidateFunc: validationUUIDorUUIDWithLocality(), |
| 23 | + ConflictsWith: []string{"domain"}, |
| 24 | + } |
| 25 | + dsSchema["organization_id"] = organizationIDOptionalSchema() |
| 26 | + dsSchema["project_id"] = &schema.Schema{ |
| 27 | + Type: schema.TypeString, |
| 28 | + Optional: true, |
| 29 | + Description: "The project ID the resource is associated to", |
| 30 | + ValidateFunc: validationUUID(), |
| 31 | + } |
| 32 | + |
| 33 | + return &schema.Resource{ |
| 34 | + Schema: dsSchema, |
| 35 | + ReadContext: dataSourceScalewayWebhostingRead, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func dataSourceScalewayWebhostingRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 40 | + api, region, err := webhostingAPIWithRegion(d, meta) |
| 41 | + if err != nil { |
| 42 | + return diag.FromErr(err) |
| 43 | + } |
| 44 | + |
| 45 | + /* var vpcID interface{} |
| 46 | + var ok bool*/ |
| 47 | + webhostingID, ok := d.GetOk("webhosting_id") |
| 48 | + if !ok { // Get IP by region and IP address. |
| 49 | + res, err := api.ListHostings(&webhosting.ListHostingsRequest{ |
| 50 | + Region: region, |
| 51 | + Domain: expandStringPtr(d.Get("domain")), |
| 52 | + ProjectID: expandStringPtr(d.Get("project_id")), |
| 53 | + OrganizationID: expandStringPtr(d.Get("organization_id")), |
| 54 | + }, scw.WithContext(ctx)) |
| 55 | + if err != nil { |
| 56 | + return diag.FromErr(err) |
| 57 | + } |
| 58 | + |
| 59 | + for _, hosting := range res.Hostings { |
| 60 | + if hosting.Domain == d.Get("domain").(string) { |
| 61 | + if webhostingID != "" { |
| 62 | + return diag.Errorf("more than 1 hosting found with the same domain %s", d.Get("domain")) |
| 63 | + } |
| 64 | + webhostingID = hosting.ID |
| 65 | + } |
| 66 | + } |
| 67 | + if webhostingID == "" { |
| 68 | + return diag.Errorf("no hosting found with the domain %s", d.Get("domain")) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + regionalID := datasourceNewRegionalID(webhostingID, region) |
| 73 | + d.SetId(regionalID) |
| 74 | + err = d.Set("webhosting_id", regionalID) |
| 75 | + if err != nil { |
| 76 | + return diag.FromErr(err) |
| 77 | + } |
| 78 | + |
| 79 | + diags := resourceScalewayWebhostingRead(ctx, d, meta) |
| 80 | + if diags != nil { |
| 81 | + return append(diags, diag.Errorf("failed to read hosting")...) |
| 82 | + } |
| 83 | + |
| 84 | + if d.Id() == "" { |
| 85 | + return diag.Errorf("hosting (%s) not found", regionalID) |
| 86 | + } |
| 87 | + |
| 88 | + return nil |
| 89 | +} |
0 commit comments