From 50fef4022a4cf1c2cc25ba3457a175eada7a493a Mon Sep 17 00:00:00 2001 From: stelcheck Date: Fri, 18 May 2018 16:35:43 +0900 Subject: [PATCH] hack --- src/terraform-provider-ncloud/provider.go | 10 ---- .../resource_public_ip.go | 54 +++++++++++-------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/terraform-provider-ncloud/provider.go b/src/terraform-provider-ncloud/provider.go index 97a774a..09347ba 100644 --- a/src/terraform-provider-ncloud/provider.go +++ b/src/terraform-provider-ncloud/provider.go @@ -18,22 +18,12 @@ func NCloudProvider() *schema.Provider { DefaultFunc: schema.EnvDefaultFunc("NCLOUD_ACCESS_KEY_ID", nil), InputDefault: "", }, - "secret_key": { Type: schema.TypeString, Optional: true, DefaultFunc: schema.EnvDefaultFunc("NCLOUD_SECRET_ACCESS_KEY", nil), InputDefault: "", }, - "region": { - Type: schema.TypeString, - Required: true, - DefaultFunc: schema.MultiEnvDefaultFunc([]string{ - "NCLOUD_REGION", - "NCLOUD_DEFAULT_REGION", - }, nil), - InputDefault: "KO-1", - }, }, ResourcesMap: map[string]*schema.Resource{ "ncloud_instance": resourceInstance(), diff --git a/src/terraform-provider-ncloud/resource_public_ip.go b/src/terraform-provider-ncloud/resource_public_ip.go index 88c4b5a..567b5e6 100644 --- a/src/terraform-provider-ncloud/resource_public_ip.go +++ b/src/terraform-provider-ncloud/resource_public_ip.go @@ -2,11 +2,34 @@ package main import ( "fmt" + "time" "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk" "github.com/hashicorp/terraform/helper/schema" ) +func retryResourcePublicIPCreate(client *sdk.Conn, params *sdk.RequestCreatePublicIPInstance, count int) (*sdk.PublicIPInstance, error) { + + response, err := client.CreatePublicIPInstance(params) + if err != nil { + if response.ReturnCode == 10101 && count != 0 { + // Hack - retry later, we might have more servers + // and therefore it might succeed + time.Sleep(5 * time.Second) + + return retryResourcePublicIPCreate(client, params, count-1) + } + + return nil, fmt.Errorf("Failed to create public IP %s", err) + } + + if response.TotalRows < 1 { + return nil, fmt.Errorf("Received no IPs in the API response") + } + + return &response.PublicIPInstanceList[0], nil +} + // Virtual machines provided on // See: https://docs.ncloud.com/en/api_new/api_new-2-1.html func resourcePublicIP() *schema.Resource { @@ -15,6 +38,12 @@ func resourcePublicIP() *schema.Resource { Read: resourcePublicIPRead, Delete: resourcePublicIPDelete, Schema: map[string]*schema.Schema{ + "region_number": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "Region number (see https://github.com/Wizcorp/terraform-provider-ncloud/blob/master/Services.md#regions)", + }, "public_ip": &schema.Schema{ Type: schema.TypeString, Computed: true, @@ -27,34 +56,15 @@ func resourcePublicIPCreate(data *schema.ResourceData, meta interface{}) error { client := meta.(*sdk.Conn) data.Partial(true) - readReqParams := new(sdk.RequestGetServerInstanceList) - - readResponse, err := client.GetServerInstanceList(readReqParams) - if err != nil { - return fmt.Errorf("Failed to read server info %s", err) - } - - if readResponse.TotalRows < 1 { - return fmt.Errorf("Received no servers in the API response") - } - - serverInfo := readResponse.ServerInstanceList[0] - reqParams := new(sdk.RequestCreatePublicIPInstance) - reqParams.RegionNo = serverInfo.Region.RegionNo + reqParams.RegionNo = data.Get("region_number").(string) // API doc says we should be allowed to specify th zone // reqParams.ZoneNo = serverInfo.Zone.ZoneNo - response, err := client.CreatePublicIPInstance(reqParams) + ipInfo, err := retryResourcePublicIPCreate(client, reqParams, 5) if err != nil { - return fmt.Errorf("Failed to create public IP %s", err) - } - - if response.TotalRows < 1 { - return fmt.Errorf("Received no IPs in the API response") + return err } - - ipInfo := response.PublicIPInstanceList[0] data.SetId(ipInfo.PublicIPInstanceNo) return resourcePublicIPRead(data, meta)