|
| 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 | + "github.com/scaleway/scaleway-sdk-go/api/vpc/v2" |
| 9 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceScalewayVPC() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + CreateContext: resourceScalewayVPCCreate, |
| 15 | + ReadContext: resourceScalewayVPCRead, |
| 16 | + UpdateContext: resourceScalewayVPCUpdate, |
| 17 | + DeleteContext: resourceScalewayVPCDelete, |
| 18 | + Importer: &schema.ResourceImporter{ |
| 19 | + StateContext: schema.ImportStatePassthroughContext, |
| 20 | + }, |
| 21 | + SchemaVersion: 0, |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "name": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Optional: true, |
| 26 | + Description: "The name of the VPC", |
| 27 | + Computed: true, |
| 28 | + }, |
| 29 | + "tags": { |
| 30 | + Type: schema.TypeList, |
| 31 | + Optional: true, |
| 32 | + Description: "The tags associated with the VPC", |
| 33 | + Elem: &schema.Schema{ |
| 34 | + Type: schema.TypeString, |
| 35 | + }, |
| 36 | + }, |
| 37 | + "project_id": projectIDSchema(), |
| 38 | + "region": regionSchema(), |
| 39 | + // Computed elements |
| 40 | + "organization_id": organizationIDSchema(), |
| 41 | + "is_default": { |
| 42 | + Type: schema.TypeBool, |
| 43 | + Computed: true, |
| 44 | + Description: "Defines whether the VPC is the default one for its Project", |
| 45 | + }, |
| 46 | + "created_at": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + Description: "The date and time of the creation of the private network", |
| 50 | + }, |
| 51 | + "updated_at": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + Description: "The date and time of the last update of the private network", |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func resourceScalewayVPCCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 61 | + vpcAPI, region, err := vpcAPIWithRegion(d, meta) |
| 62 | + if err != nil { |
| 63 | + return diag.FromErr(err) |
| 64 | + } |
| 65 | + |
| 66 | + res, err := vpcAPI.CreateVPC(&vpc.CreateVPCRequest{ |
| 67 | + Name: expandOrGenerateString(d.Get("name"), "vpc"), |
| 68 | + Tags: expandStrings(d.Get("tags")), |
| 69 | + ProjectID: d.Get("project_id").(string), |
| 70 | + Region: region, |
| 71 | + }, scw.WithContext(ctx)) |
| 72 | + if err != nil { |
| 73 | + return diag.FromErr(err) |
| 74 | + } |
| 75 | + |
| 76 | + d.SetId(newRegionalIDString(region, res.ID)) |
| 77 | + |
| 78 | + return resourceScalewayVPCRead(ctx, d, meta) |
| 79 | +} |
| 80 | + |
| 81 | +func resourceScalewayVPCRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 82 | + vpcAPI, region, ID, err := vpcAPIWithRegionAndID(meta, d.Id()) |
| 83 | + if err != nil { |
| 84 | + return diag.FromErr(err) |
| 85 | + } |
| 86 | + |
| 87 | + res, err := vpcAPI.GetVPC(&vpc.GetVPCRequest{ |
| 88 | + Region: region, |
| 89 | + VpcID: ID, |
| 90 | + }, scw.WithContext(ctx)) |
| 91 | + if err != nil { |
| 92 | + if is404Error(err) { |
| 93 | + d.SetId("") |
| 94 | + return nil |
| 95 | + } |
| 96 | + return diag.FromErr(err) |
| 97 | + } |
| 98 | + |
| 99 | + _ = d.Set("name", res.Name) |
| 100 | + _ = d.Set("organization_id", res.OrganizationID) |
| 101 | + _ = d.Set("project_id", res.ProjectID) |
| 102 | + _ = d.Set("created_at", flattenTime(res.CreatedAt)) |
| 103 | + _ = d.Set("updated_at", flattenTime(res.UpdatedAt)) |
| 104 | + _ = d.Set("is_default", res.IsDefault) |
| 105 | + _ = d.Set("region", region) |
| 106 | + |
| 107 | + if len(res.Tags) > 0 { |
| 108 | + _ = d.Set("tags", res.Tags) |
| 109 | + } |
| 110 | + |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func resourceScalewayVPCUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 115 | + vpcAPI, region, ID, err := vpcAPIWithRegionAndID(meta, d.Id()) |
| 116 | + if err != nil { |
| 117 | + return diag.FromErr(err) |
| 118 | + } |
| 119 | + |
| 120 | + _, err = vpcAPI.UpdateVPC(&vpc.UpdateVPCRequest{ |
| 121 | + VpcID: ID, |
| 122 | + Region: region, |
| 123 | + Name: scw.StringPtr(d.Get("name").(string)), |
| 124 | + Tags: expandUpdatedStringsPtr(d.Get("tags")), |
| 125 | + }, scw.WithContext(ctx)) |
| 126 | + if err != nil { |
| 127 | + return diag.FromErr(err) |
| 128 | + } |
| 129 | + |
| 130 | + return resourceScalewayVPCRead(ctx, d, meta) |
| 131 | +} |
| 132 | + |
| 133 | +func resourceScalewayVPCDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 134 | + vpcAPI, region, ID, err := vpcAPIWithRegionAndID(meta, d.Id()) |
| 135 | + if err != nil { |
| 136 | + return diag.FromErr(err) |
| 137 | + } |
| 138 | + |
| 139 | + err = vpcAPI.DeleteVPC(&vpc.DeleteVPCRequest{ |
| 140 | + Region: region, |
| 141 | + VpcID: ID, |
| 142 | + }, scw.WithContext(ctx)) |
| 143 | + if err != nil { |
| 144 | + return diag.FromErr(err) |
| 145 | + } |
| 146 | + |
| 147 | + return nil |
| 148 | +} |
0 commit comments