|
| 1 | +package scaleway |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + vpcgw "github.com/scaleway/scaleway-sdk-go/api/vpcgw/v1beta1" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 11 | +) |
| 12 | + |
| 13 | +func resourceScalewayVPCPublicGatewayIP() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + CreateContext: resourceScalewayVPCPublicGatewayIPCreate, |
| 16 | + ReadContext: resourceScalewayVPCPublicGatewayIPRead, |
| 17 | + UpdateContext: resourceScalewayVPCPublicGatewayIPUpdate, |
| 18 | + DeleteContext: resourceScalewayVPCPublicGatewayIPDelete, |
| 19 | + Importer: &schema.ResourceImporter{ |
| 20 | + StateContext: schema.ImportStatePassthroughContext, |
| 21 | + }, |
| 22 | + SchemaVersion: 0, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "ip_id": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Optional: true, |
| 27 | + Computed: true, |
| 28 | + Description: "attach an existing IP to the gateway", |
| 29 | + }, |
| 30 | + "tags": { |
| 31 | + Type: schema.TypeList, |
| 32 | + Optional: true, |
| 33 | + Description: "The tags associated with public gateway", |
| 34 | + Elem: &schema.Schema{ |
| 35 | + Type: schema.TypeString, |
| 36 | + }, |
| 37 | + }, |
| 38 | + "project_id": projectIDSchema(), |
| 39 | + "zone": zoneSchema(), |
| 40 | + // Computed elements |
| 41 | + "organization_id": organizationIDSchema(), |
| 42 | + "created_at": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Computed: true, |
| 45 | + Description: "The date and time of the creation of the public gateway", |
| 46 | + }, |
| 47 | + "updated_at": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + Description: "The date and time of the last update of the public gateway", |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func resourceScalewayVPCPublicGatewayIPCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 57 | + vpcgwAPI, zone, err := vpcgwAPIWithZone(d, meta) |
| 58 | + if err != nil { |
| 59 | + return diag.FromErr(err) |
| 60 | + } |
| 61 | + |
| 62 | + req := &vpcgw.CreateIPRequest{ |
| 63 | + Tags: expandStrings(d.Get("tags")), |
| 64 | + ProjectID: d.Get("project_id").(string), |
| 65 | + Zone: zone, |
| 66 | + } |
| 67 | + |
| 68 | + res, err := vpcgwAPI.CreateIP(req, scw.WithContext(ctx)) |
| 69 | + if err != nil { |
| 70 | + return diag.FromErr(err) |
| 71 | + } |
| 72 | + |
| 73 | + d.SetId(newZonedIDString(zone, res.ID)) |
| 74 | + |
| 75 | + return resourceScalewayVPCPublicGatewayIPRead(ctx, d, meta) |
| 76 | +} |
| 77 | + |
| 78 | +func resourceScalewayVPCPublicGatewayIPRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 79 | + vpcgwAPI, zone, ID, err := vpcgwAPIWithZoneAndID(meta, d.Id()) |
| 80 | + if err != nil { |
| 81 | + return diag.FromErr(err) |
| 82 | + } |
| 83 | + |
| 84 | + ip, err := vpcgwAPI.GetIP(&vpcgw.GetIPRequest{ |
| 85 | + IPID: ID, |
| 86 | + Zone: zone, |
| 87 | + }, scw.WithContext(ctx)) |
| 88 | + if err != nil { |
| 89 | + if is404Error(err) { |
| 90 | + d.SetId("") |
| 91 | + return nil |
| 92 | + } |
| 93 | + return diag.FromErr(err) |
| 94 | + } |
| 95 | + |
| 96 | + _ = d.Set("organization_id", ip.OrganizationID) |
| 97 | + _ = d.Set("project_id", ip.ProjectID) |
| 98 | + _ = d.Set("created_at", ip.CreatedAt.Format(time.RFC3339)) |
| 99 | + _ = d.Set("updated_at", ip.UpdatedAt.Format(time.RFC3339)) |
| 100 | + _ = d.Set("zone", zone) |
| 101 | + _ = d.Set("tags", ip.Tags) |
| 102 | + _ = d.Set("ip_id", ip.ID) |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func resourceScalewayVPCPublicGatewayIPUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 108 | + vpcgwAPI, zone, ID, err := vpcgwAPIWithZoneAndID(meta, d.Id()) |
| 109 | + if err != nil { |
| 110 | + return diag.FromErr(err) |
| 111 | + } |
| 112 | + |
| 113 | + if d.HasChanges("tags") { |
| 114 | + updateRequest := &vpcgw.UpdateIPRequest{ |
| 115 | + IPID: ID, |
| 116 | + Zone: zone, |
| 117 | + Tags: scw.StringsPtr(expandStrings(d.Get("tags"))), |
| 118 | + } |
| 119 | + |
| 120 | + _, err = vpcgwAPI.UpdateIP(updateRequest, scw.WithContext(ctx)) |
| 121 | + if err != nil { |
| 122 | + return diag.FromErr(err) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return resourceScalewayVPCPublicGatewayIPRead(ctx, d, meta) |
| 127 | +} |
| 128 | + |
| 129 | +func resourceScalewayVPCPublicGatewayIPDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 130 | + vpcgwAPI, zone, ID, err := vpcgwAPIWithZoneAndID(meta, d.Id()) |
| 131 | + if err != nil { |
| 132 | + return diag.FromErr(err) |
| 133 | + } |
| 134 | + |
| 135 | + err = vpcgwAPI.DeleteIP(&vpcgw.DeleteIPRequest{ |
| 136 | + IPID: ID, |
| 137 | + Zone: zone, |
| 138 | + }, scw.WithContext(ctx)) |
| 139 | + |
| 140 | + if err != nil && !is404Error(err) { |
| 141 | + return diag.FromErr(err) |
| 142 | + } |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
0 commit comments