|
| 1 | +package gcore |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net" |
| 8 | + "time" |
| 9 | + |
| 10 | + gcorecloud "github.com/G-Core/gcorelabscloud-go" |
| 11 | + "github.com/G-Core/gcorelabscloud-go/gcore/floatingip/v1/floatingips" |
| 12 | + "github.com/G-Core/gcorelabscloud-go/gcore/task/v1/tasks" |
| 13 | + "github.com/hashicorp/go-cty/cty" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 15 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + floatingIPsPoint = "floatingips" |
| 20 | + FloatingIPCreateTimeout = 1200 |
| 21 | +) |
| 22 | + |
| 23 | +func resourceFloatingIP() *schema.Resource { |
| 24 | + return &schema.Resource{ |
| 25 | + CreateContext: resourceFloatingIPCreate, |
| 26 | + ReadContext: resourceFloatingIPRead, |
| 27 | + UpdateContext: resourceFloatingIPUpdate, |
| 28 | + DeleteContext: resourceFloatingIPDelete, |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "project_id": &schema.Schema{ |
| 31 | + Type: schema.TypeInt, |
| 32 | + Optional: true, |
| 33 | + ForceNew: true, |
| 34 | + ExactlyOneOf: []string{ |
| 35 | + "project_id", |
| 36 | + "project_name", |
| 37 | + }, |
| 38 | + }, |
| 39 | + "region_id": &schema.Schema{ |
| 40 | + Type: schema.TypeInt, |
| 41 | + Optional: true, |
| 42 | + ForceNew: true, |
| 43 | + ExactlyOneOf: []string{ |
| 44 | + "region_id", |
| 45 | + "region_name", |
| 46 | + }, |
| 47 | + }, |
| 48 | + "project_name": &schema.Schema{ |
| 49 | + Type: schema.TypeString, |
| 50 | + Optional: true, |
| 51 | + ForceNew: true, |
| 52 | + ExactlyOneOf: []string{ |
| 53 | + "project_id", |
| 54 | + "project_name", |
| 55 | + }, |
| 56 | + }, |
| 57 | + "region_name": &schema.Schema{ |
| 58 | + Type: schema.TypeString, |
| 59 | + Optional: true, |
| 60 | + ForceNew: true, |
| 61 | + ExactlyOneOf: []string{ |
| 62 | + "region_id", |
| 63 | + "region_name", |
| 64 | + }, |
| 65 | + }, |
| 66 | + "status": &schema.Schema{ |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + }, |
| 70 | + "fixed_ip_address": &schema.Schema{ |
| 71 | + Type: schema.TypeString, |
| 72 | + Optional: true, |
| 73 | + ValidateDiagFunc: func(val interface{}, key cty.Path) diag.Diagnostics { |
| 74 | + v := val.(string) |
| 75 | + ip := net.ParseIP(v) |
| 76 | + if ip != nil { |
| 77 | + return diag.Diagnostics{} |
| 78 | + } |
| 79 | + |
| 80 | + return diag.FromErr(fmt.Errorf("%q must be a valid ip, got: %s", key, v)) |
| 81 | + }, |
| 82 | + }, |
| 83 | + "floating_ip_address": &schema.Schema{ |
| 84 | + Type: schema.TypeString, |
| 85 | + Computed: true, |
| 86 | + }, |
| 87 | + "router_id": &schema.Schema{ |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + }, |
| 91 | + "port_id": &schema.Schema{ |
| 92 | + Type: schema.TypeString, |
| 93 | + Optional: true, |
| 94 | + }, |
| 95 | + "updated_at": &schema.Schema{ |
| 96 | + Type: schema.TypeString, |
| 97 | + Computed: true, |
| 98 | + }, |
| 99 | + "created_at": &schema.Schema{ |
| 100 | + Type: schema.TypeString, |
| 101 | + Computed: true, |
| 102 | + }, |
| 103 | + "last_updated": &schema.Schema{ |
| 104 | + Type: schema.TypeString, |
| 105 | + Optional: true, |
| 106 | + Computed: true, |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func resourceFloatingIPCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 113 | + log.Println("[DEBUG] Start FloatingIP creating") |
| 114 | + var diags diag.Diagnostics |
| 115 | + config := m.(*Config) |
| 116 | + provider := config.Provider |
| 117 | + |
| 118 | + client, err := CreateClient(provider, d, floatingIPsPoint, versionPointV1) |
| 119 | + if err != nil { |
| 120 | + return diag.FromErr(err) |
| 121 | + } |
| 122 | + |
| 123 | + opts := floatingips.CreateOpts{ |
| 124 | + PortID: d.Get("port_id").(string), |
| 125 | + FixedIPAddress: net.ParseIP(d.Get("fixed_ip_address").(string)), |
| 126 | + } |
| 127 | + |
| 128 | + results, err := floatingips.Create(client, opts).Extract() |
| 129 | + if err != nil { |
| 130 | + return diag.FromErr(err) |
| 131 | + } |
| 132 | + |
| 133 | + taskID := results.Tasks[0] |
| 134 | + floatingIPID, err := tasks.WaitTaskAndReturnResult(client, taskID, true, FloatingIPCreateTimeout, func(task tasks.TaskID) (interface{}, error) { |
| 135 | + taskInfo, err := tasks.Get(client, string(task)).Extract() |
| 136 | + if err != nil { |
| 137 | + return nil, fmt.Errorf("cannot get task with ID: %s. Error: %w", task, err) |
| 138 | + } |
| 139 | + floatingIPID, err := floatingips.ExtractFloatingIPIDFromTask(taskInfo) |
| 140 | + if err != nil { |
| 141 | + return nil, fmt.Errorf("cannot retrieve FloatingIP ID from task info: %w", err) |
| 142 | + } |
| 143 | + return floatingIPID, nil |
| 144 | + }) |
| 145 | + |
| 146 | + log.Printf("[DEBUG] FloatingIP id (%s)", floatingIPID) |
| 147 | + if err != nil { |
| 148 | + return diag.FromErr(err) |
| 149 | + } |
| 150 | + |
| 151 | + d.SetId(floatingIPID.(string)) |
| 152 | + resourceFloatingIPRead(ctx, d, m) |
| 153 | + |
| 154 | + log.Printf("[DEBUG] Finish FloatingIP creating (%s)", floatingIPID) |
| 155 | + return diags |
| 156 | +} |
| 157 | + |
| 158 | +func resourceFloatingIPRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 159 | + log.Println("[DEBUG] Start FloatingIP reading") |
| 160 | + var diags diag.Diagnostics |
| 161 | + config := m.(*Config) |
| 162 | + provider := config.Provider |
| 163 | + |
| 164 | + client, err := CreateClient(provider, d, floatingIPsPoint, versionPointV1) |
| 165 | + if err != nil { |
| 166 | + return diag.FromErr(err) |
| 167 | + } |
| 168 | + |
| 169 | + floatingIP, err := floatingips.Get(client, d.Id()).Extract() |
| 170 | + if err != nil { |
| 171 | + return diag.FromErr(err) |
| 172 | + } |
| 173 | + |
| 174 | + if floatingIP.FixedIPAddress != nil { |
| 175 | + d.Set("fixed_ip_address", floatingIP.FixedIPAddress.String()) |
| 176 | + } else { |
| 177 | + d.Set("fixed_ip_address", "") |
| 178 | + } |
| 179 | + |
| 180 | + d.Set("project_id", floatingIP.ProjectID) |
| 181 | + d.Set("region_id", floatingIP.RegionID) |
| 182 | + d.Set("status", floatingIP.Status) |
| 183 | + d.Set("port_id", floatingIP.PortID) |
| 184 | + d.Set("router_id", floatingIP.RouterID) |
| 185 | + d.Set("floating_ip_address", floatingIP.FloatingIPAddress.String()) |
| 186 | + |
| 187 | + log.Println("[DEBUG] Finish FloatingIP reading") |
| 188 | + return diags |
| 189 | +} |
| 190 | + |
| 191 | +func resourceFloatingIPUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 192 | + log.Println("[DEBUG] Start FloatingIP updating") |
| 193 | + config := m.(*Config) |
| 194 | + provider := config.Provider |
| 195 | + |
| 196 | + client, err := CreateClient(provider, d, floatingIPsPoint, versionPointV1) |
| 197 | + if err != nil { |
| 198 | + return diag.FromErr(err) |
| 199 | + } |
| 200 | + |
| 201 | + if d.HasChange("fixed_ip_address") || d.HasChange("port_id") { |
| 202 | + oldFixedIP, newFixedIP := d.GetChange("fixed_ip_address") |
| 203 | + oldPortID, newPortID := d.GetChange("port_id") |
| 204 | + if oldPortID.(string) != "" || oldFixedIP.(string) != "" { |
| 205 | + _, err := floatingips.UnAssign(client, d.Id()).Extract() |
| 206 | + if err != nil { |
| 207 | + return diag.FromErr(err) |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + if newPortID.(string) != "" || newFixedIP.(string) != "" { |
| 212 | + opts := floatingips.CreateOpts{ |
| 213 | + PortID: d.Get("port_id").(string), |
| 214 | + FixedIPAddress: net.ParseIP(d.Get("fixed_ip_address").(string)), |
| 215 | + } |
| 216 | + |
| 217 | + _, err = floatingips.Assign(client, d.Id(), opts).Extract() |
| 218 | + if err != nil { |
| 219 | + return diag.FromErr(err) |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + d.Set("last_updated", time.Now().Format(time.RFC850)) |
| 224 | + } |
| 225 | + |
| 226 | + return resourceFloatingIPRead(ctx, d, m) |
| 227 | +} |
| 228 | + |
| 229 | +func resourceFloatingIPDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 230 | + log.Println("[DEBUG] Start FloatingIP deleting") |
| 231 | + var diags diag.Diagnostics |
| 232 | + config := m.(*Config) |
| 233 | + provider := config.Provider |
| 234 | + |
| 235 | + client, err := CreateClient(provider, d, floatingIPsPoint, versionPointV1) |
| 236 | + if err != nil { |
| 237 | + return diag.FromErr(err) |
| 238 | + } |
| 239 | + |
| 240 | + id := d.Id() |
| 241 | + results, err := floatingips.Delete(client, id).Extract() |
| 242 | + if err != nil { |
| 243 | + return diag.FromErr(err) |
| 244 | + } |
| 245 | + |
| 246 | + taskID := results.Tasks[0] |
| 247 | + _, err = tasks.WaitTaskAndReturnResult(client, taskID, true, FloatingIPCreateTimeout, func(task tasks.TaskID) (interface{}, error) { |
| 248 | + _, err := floatingips.Get(client, id).Extract() |
| 249 | + if err == nil { |
| 250 | + return nil, fmt.Errorf("cannot delete floating ip with ID: %s", id) |
| 251 | + } |
| 252 | + switch err.(type) { |
| 253 | + case gcorecloud.ErrDefault404: |
| 254 | + return nil, nil |
| 255 | + default: |
| 256 | + return nil, err |
| 257 | + } |
| 258 | + }) |
| 259 | + if err != nil { |
| 260 | + return diag.FromErr(err) |
| 261 | + } |
| 262 | + |
| 263 | + d.SetId("") |
| 264 | + log.Printf("[DEBUG] Finish of FloatingIP deleting") |
| 265 | + return diags |
| 266 | +} |
0 commit comments