|
| 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 | + "github.com/scaleway/scaleway-sdk-go/api/vpc/v2" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 11 | +) |
| 12 | + |
| 13 | +func dataSourceScalewayVPC() *schema.Resource { |
| 14 | + dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayVPC().Schema) |
| 15 | + |
| 16 | + addOptionalFieldsToSchema(dsSchema, "name", "is_default", "region") |
| 17 | + |
| 18 | + dsSchema["name"].ConflictsWith = []string{"vpc_id"} |
| 19 | + dsSchema["vpc_id"] = &schema.Schema{ |
| 20 | + Type: schema.TypeString, |
| 21 | + Optional: true, |
| 22 | + Description: "The ID of the VPC", |
| 23 | + ValidateFunc: validationUUIDorUUIDWithLocality(), |
| 24 | + ConflictsWith: []string{"name"}, |
| 25 | + } |
| 26 | + dsSchema["organization_id"] = organizationIDOptionalSchema() |
| 27 | + dsSchema["project_id"] = &schema.Schema{ |
| 28 | + Type: schema.TypeString, |
| 29 | + Optional: true, |
| 30 | + Description: "The project ID the resource is associated to", |
| 31 | + ValidateFunc: validationUUID(), |
| 32 | + } |
| 33 | + |
| 34 | + return &schema.Resource{ |
| 35 | + Schema: dsSchema, |
| 36 | + ReadContext: dataSourceScalewayVPCRead, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func dataSourceScalewayVPCRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 41 | + vpcAPI, region, err := vpcAPIWithRegion(d, meta) |
| 42 | + if err != nil { |
| 43 | + return diag.FromErr(err) |
| 44 | + } |
| 45 | + |
| 46 | + var vpcID interface{} |
| 47 | + var ok bool |
| 48 | + |
| 49 | + if d.Get("is_default").(bool) { |
| 50 | + request := &vpc.ListVPCsRequest{ |
| 51 | + IsDefault: expandBoolPtr(d.Get("is_default").(bool)), |
| 52 | + Region: region, |
| 53 | + } |
| 54 | + |
| 55 | + res, err := vpcAPI.ListVPCs(request, scw.WithContext(ctx)) |
| 56 | + if err != nil { |
| 57 | + return diag.FromErr(err) |
| 58 | + } |
| 59 | + |
| 60 | + vpcID = newRegionalIDString(region, res.Vpcs[0].ID) |
| 61 | + } else { |
| 62 | + vpcID, ok = d.GetOk("vpc_id") |
| 63 | + if !ok { |
| 64 | + request := &vpc.ListVPCsRequest{ |
| 65 | + Name: expandStringPtr(d.Get("name").(string)), |
| 66 | + Region: region, |
| 67 | + ProjectID: expandStringPtr(d.Get("project_id")), |
| 68 | + OrganizationID: expandStringPtr(d.Get("organization_id")), |
| 69 | + } |
| 70 | + |
| 71 | + res, err := vpcAPI.ListVPCs(request, scw.WithContext(ctx)) |
| 72 | + if err != nil { |
| 73 | + return diag.FromErr(err) |
| 74 | + } |
| 75 | + |
| 76 | + for _, v := range res.Vpcs { |
| 77 | + if v.Name == d.Get("name").(string) { |
| 78 | + if vpcID != "" { |
| 79 | + return diag.FromErr(fmt.Errorf("more than 1 VPC found with the same name %s", d.Get("name"))) |
| 80 | + } |
| 81 | + vpcID = newRegionalIDString(region, v.ID) |
| 82 | + } |
| 83 | + } |
| 84 | + if res.TotalCount == 0 { |
| 85 | + return diag.FromErr(fmt.Errorf("no VPC found with the name %s", d.Get("name"))) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + regionalID := datasourceNewRegionalizedID(vpcID, region) |
| 91 | + d.SetId(regionalID) |
| 92 | + err = d.Set("vpc_id", regionalID) |
| 93 | + if err != nil { |
| 94 | + return diag.FromErr(err) |
| 95 | + } |
| 96 | + |
| 97 | + diags := resourceScalewayVPCRead(ctx, d, meta) |
| 98 | + if diags != nil { |
| 99 | + return append(diags, diag.Errorf("failed to read VPC")...) |
| 100 | + } |
| 101 | + |
| 102 | + if d.Id() == "" { |
| 103 | + return diag.Errorf("VPC (%s) not found", regionalID) |
| 104 | + } |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
0 commit comments