Skip to content

Commit

Permalink
create ip then assign them to servers
Browse files Browse the repository at this point in the history
  • Loading branch information
stelcheck committed May 17, 2018
1 parent 47c7680 commit c911f84
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/terraform-provider-ncloud/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ func resourceInstance() *schema.Resource {
Description: "script to run at first boot",
Default: false,
},
"public_ip_instance": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Public IP ID",
Default: "",
},
"public_ip": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -120,6 +127,19 @@ func resourceInstanceCreate(data *schema.ResourceData, meta interface{}) error {

waitForServerStatus(client, data.Id(), "RUN")

publicIP := data.Get("public_ip_instance").(string)
if publicIP != "" {
associateReqParams := new(sdk.RequestAssociatePublicIP)
associateReqParams.PublicIPInstanceNo = publicIP
associateReqParams.ServerInstanceNo = data.Id()

_, err = client.AssociatePublicIP(associateReqParams)
if err != nil {
return fmt.Errorf("Failed to associate public IP %s", err)
}
}
data.SetPartial("public_ip_instance")

return resourceInstanceRead(data, meta)
}

Expand Down Expand Up @@ -160,6 +180,8 @@ func resourceInstanceDelete(data *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Failed to disassociate IP with ID %s from server %s: %s", publicIP, data.Id(), err)
}

waitForPublicIPDetach(client, publicIPInstance.PublicIPInstanceNo)

break
}
}
Expand Down
10 changes: 0 additions & 10 deletions src/terraform-provider-ncloud/resource_public_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ func resourcePublicIP() *schema.Resource {
Read: resourcePublicIPRead,
Delete: resourcePublicIPDelete,
Schema: map[string]*schema.Schema{
"server_id": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
Description: "Product code (see https://github.com/Wizcorp/terraform-provider-ncloud/blob/master/Services.md#servers-server_product_code)",
},
"public_ip": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Expand All @@ -33,8 +27,6 @@ func resourcePublicIPCreate(data *schema.ResourceData, meta interface{}) error {
client := meta.(*sdk.Conn)
data.Partial(true)

serverID := data.Get("server_id").(string)

readReqParams := new(sdk.RequestGetServerInstanceList)

readResponse, err := client.GetServerInstanceList(readReqParams)
Expand All @@ -49,7 +41,6 @@ func resourcePublicIPCreate(data *schema.ResourceData, meta interface{}) error {
serverInfo := readResponse.ServerInstanceList[0]

reqParams := new(sdk.RequestCreatePublicIPInstance)
reqParams.ServerInstanceNo = serverID
reqParams.RegionNo = serverInfo.Region.RegionNo
// API doc says we should be allowed to specify th zone
// reqParams.ZoneNo = serverInfo.Zone.ZoneNo
Expand All @@ -65,7 +56,6 @@ func resourcePublicIPCreate(data *schema.ResourceData, meta interface{}) error {

ipInfo := response.PublicIPInstanceList[0]
data.SetId(ipInfo.PublicIPInstanceNo)
data.SetPartial("server_id")

return resourcePublicIPRead(data, meta)
}
Expand Down

0 comments on commit c911f84

Please sign in to comment.