Skip to content

Commit

Permalink
hack
Browse files Browse the repository at this point in the history
  • Loading branch information
stelcheck committed May 18, 2018
1 parent 8700e55 commit 50fef40
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
10 changes: 0 additions & 10 deletions src/terraform-provider-ncloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
54 changes: 32 additions & 22 deletions src/terraform-provider-ncloud/resource_public_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 50fef40

Please sign in to comment.