Skip to content

Commit

Permalink
Added resource for ibm_pi_snapshot + cleaned up pi_instance (#1867)
Browse files Browse the repository at this point in the history
* Added resource for ibm_pi_snapshot + cleaned up pi_instance

* Fixed review comments

Co-authored-by: suraj <suraj@us.ibm.com>
  • Loading branch information
surajsub and surajibm authored Sep 14, 2020
1 parent 0da8d47 commit bf17cba
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 121 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.12

require (
github.com/Bowery/prompt v0.0.0-20190916142128-fa8279994f75 // indirect
github.com/IBM-Cloud/bluemix-go v0.0.0-20200907101940-45dc51a40515
github.com/IBM-Cloud/power-go-client v1.0.46
github.com/IBM-Cloud/bluemix-go v0.0.0-20200903083903-3b405c2db0da
github.com/IBM-Cloud/power-go-client v1.0.48
github.com/IBM/apigateway-go-sdk v0.0.0-20200414212859-416e5948678a
github.com/IBM/dns-svcs-go-sdk v0.0.3
github.com/IBM/go-sdk-core v1.1.0
Expand Down
5 changes: 5 additions & 0 deletions ibm/data_source_ibm_pi_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func dataSourceIBMPIImage() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"storage_type": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -74,6 +78,7 @@ func dataSourceIBMPIImagesRead(d *schema.ResourceData, meta interface{}) error {
d.Set("architecture", imagedata.Specifications.Architecture)
d.Set("hypervisor", imagedata.Specifications.HypervisorType)
d.Set("operatingsystem", imagedata.Specifications.OperatingSystem)
d.Set("storage_type", imagedata.StorageType)

return nil

Expand Down
16 changes: 11 additions & 5 deletions ibm/data_source_ibm_pi_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ func dataSourceIBMPIImages() *schema.Resource {
},
"state": {
Type: schema.TypeString,
Computed: true},
Computed: true,
},
"storage_type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -94,10 +99,11 @@ func flattenStockImages(list []*models.ImageReference) []map[string]interface{}
for _, i := range list {

l := map[string]interface{}{
"id": *i.ImageID,
"state": *i.State,
"href": *i.Href,
"name": *i.Name,
"id": *i.ImageID,
"state": *i.State,
"href": *i.Href,
"name": *i.Name,
"storage_type": *i.StorageType,
}

result = append(result, l)
Expand Down
20 changes: 11 additions & 9 deletions ibm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,17 @@ func Provider() terraform.ResourceProvider {

//Added for Power Colo

"ibm_pi_key": resourceIBMPIKey(),
"ibm_pi_volume": resourceIBMPIVolume(),
"ibm_pi_network": resourceIBMPINetwork(),
"ibm_pi_instance": resourceIBMPIInstance(),
"ibm_pi_operations": resourceIBMPIIOperations(),
"ibm_pi_volume_attach": resourceIBMPIVolumeAttach(),
"ibm_pi_capture": resourceIBMPICapture(),
"ibm_pi_image": resourceIBMPIImage(),
"ibm_pi_network_port": resourceIBMPINetworkPort(),
"ibm_pi_key": resourceIBMPIKey(),
"ibm_pi_volume": resourceIBMPIVolume(),
"ibm_pi_network": resourceIBMPINetwork(),
"ibm_pi_instance": resourceIBMPIInstance(),
"ibm_pi_operations": resourceIBMPIIOperations(),
"ibm_pi_volume_attach": resourceIBMPIVolumeAttach(),
"ibm_pi_capture": resourceIBMPICapture(),
"ibm_pi_image": resourceIBMPIImage(),
"ibm_pi_network_port": resourceIBMPINetworkPort(),
"ibm_pi_snapshot": resourceIBMPISnapshot(),
"ibm_pi_network_port_attach": resourceIBMPINetworkPortAttach(),

//Private DNS related resources
"ibm_dns_zone": resourceIBMPrivateDNSZone(),
Expand Down
11 changes: 7 additions & 4 deletions ibm/resource_ibm_pi_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ func resourceIBMPIInstanceCreate(d *schema.ResourceData, meta interface{}) error
return err
}
powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)

name := d.Get(helpers.PIInstanceName).(string)
sshkey := d.Get(helpers.PIInstanceSSHKeyName).(string)
mem := d.Get(helpers.PIInstanceMemory).(float64)
Expand Down Expand Up @@ -340,11 +339,15 @@ func resourceIBMPIInstanceCreate(d *schema.ResourceData, meta interface{}) error
body.PinPolicy = models.PinPolicy(pinpolicy)
}

assigned_virtual_cores := int64(d.Get(helpers.PIVirtualCoresAssigned).(int))
if assigned_virtual_cores != 0 {
if d.Get(helpers.PIVirtualCoresAssigned) != "" && d.Get(helpers.PIVirtualCoresAssigned) != 0 {
//cores, err := strconv.Atoi(d.Get(helpers.PIVirtualCoresAssigned).(string))
//if err != nil {
// fmt.Errorf("failed to convert %v", err)
//}
assigned_virtual_cores := int64(d.Get(helpers.PIVirtualCoresAssigned).(int))
body.VirtualCores = &models.VirtualCores{Assigned: &assigned_virtual_cores}
} else {
log.Printf("virutal cores is not provided")
log.Printf("Virtual cores is not provided")
}

client := st.NewIBMPIInstanceClient(sess, powerinstanceid)
Expand Down
51 changes: 40 additions & 11 deletions ibm/resource_ibm_pi_network_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ibm

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"log"
"time"

Expand Down Expand Up @@ -44,12 +45,7 @@ func resourceIBMPINetworkPort() *schema.Resource {
Optional: true,
},

/*
"description": "",
"ipAddress": "172.31.96.93",
"macAddress": "fa:16:3e:30:b9:64",
"portID": "6c9d0e42-73f3-492f-840d-3d4a7a573014",
*/
//Computed Attributes

helpers.PINetworkPortIPAddress: {
Type: schema.TypeString,
Expand All @@ -68,9 +64,6 @@ func resourceIBMPINetworkPort() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

//Computed Attributes

},
}
}
Expand All @@ -83,12 +76,13 @@ func resourceIBMPINetworkPortCreate(d *schema.ResourceData, meta interface{}) er
powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)
networkname := d.Get(helpers.PINetworkName).(string)
description := d.Get(helpers.PINetworkPortDescription).(string)

ipaddress := d.Get(helpers.PINetworkPortIPAddress).(string)

nwportBody := &models.NetworkPortCreate{Description: description}

if ipaddress != "" {
log.Printf("No IP address provided. ")
log.Printf("IP address provided. ")
nwportBody.IPAddress = ipaddress
}

Expand Down Expand Up @@ -154,12 +148,13 @@ func resourceIBMPINetworkPortUpdate(data *schema.ResourceData, meta interface{})
func resourceIBMPINetworkPortDelete(d *schema.ResourceData, meta interface{}) error {

log.Printf("Calling the network delete functions. ")
powernetworkname := d.Get(helpers.PINetworkName).(string)
sess, err := meta.(ClientSession).IBMPISession()
if err != nil {
return err
}
parts, err := idParts(d.Id())
powernetworkname := d.Get(helpers.PINetworkName).(string)

if err != nil {
return err
}
Expand Down Expand Up @@ -199,3 +194,37 @@ func resourceIBMPINetworkPortExists(d *schema.ResourceData, meta interface{}) (b
}
return *network.NetworkID == parts[1], nil
}

func isWaitForIBMPINetworkPortAvailable(client *st.IBMPINetworkClient, id string, timeout time.Duration, powerinstanceid, networkname string) (interface{}, error) {
log.Printf("Waiting for Power Network (%s) that was created for Network Zone (%s) to be available.", id, networkname)

stateConf := &resource.StateChangeConf{
Pending: []string{"retry", helpers.PINetworkProvisioning},
Target: []string{"DOWN"},
Refresh: isIBMPINetworkPortRefreshFunc(client, id, powerinstanceid, networkname),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 10 * time.Minute,
}

return stateConf.WaitForState()
}

func isIBMPINetworkPortRefreshFunc(client *st.IBMPINetworkClient, id, powerinstanceid, networkname string) resource.StateRefreshFunc {

log.Printf("Calling the IsIBMPINetwork Refresh Function....with the following id (%s) for network port and following id (%s) for network name and waiting for network to be READY", id, networkname)
return func() (interface{}, string, error) {
network, err := client.GetPort(networkname, powerinstanceid, id, getTimeOut)
if err != nil {
return nil, "", err
}

if &network.PortID != nil {
//if network.State == "available" {
log.Printf(" The port has been created with the following ip address and attached to an instance ")
return network, "DOWN", nil
}

return network, helpers.PINetworkProvisioning, nil
}
}
99 changes: 68 additions & 31 deletions ibm/resource_ibm_pi_network_port_attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,22 @@ func resourceIBMPINetworkPortAttach() *schema.Resource {
},

helpers.PIInstanceName: {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
Description: "Instance name to attach the network port to",
},

helpers.PINetworkName: {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
Description: "Network Name - This is the subnet name in the Cloud instance",
},

"description": {
Type: schema.TypeString,
Optional: true,
helpers.PINetworkPortDescription: {
Type: schema.TypeString,
Optional: true,
Description: "A human readable description for this network Port",
Default: "Port Created via Terraform",
},
},
}
Expand All @@ -67,7 +71,7 @@ func resourceIBMPINetworkPortAttachCreate(d *schema.ResourceData, meta interface
networkname := d.Get(helpers.PINetworkName).(string)
portid := d.Get("port_id").(string)
instancename := d.Get(helpers.PIInstanceName).(string)
description := d.Get("description").(string)
description := d.Get(helpers.PINetworkPortDescription).(string)
client := st.NewIBMPINetworkClient(sess, powerinstanceid)

log.Printf("Printing the input to the resource powerinstance [%s] and network name [%s] and the portid [%s]", powerinstanceid, networkname, portid)
Expand All @@ -86,7 +90,7 @@ func resourceIBMPINetworkPortAttachCreate(d *schema.ResourceData, meta interface
log.Printf("[DEBUG] err %s", err)
return err
}
_, err = isWaitForIBMPINetworkPortAvailable(client, IBMPINetworkPortID, d.Timeout(schema.TimeoutCreate), powerinstanceid, networkname)
_, err = isWaitForIBMPINetworkPortAttachAvailable(client, IBMPINetworkPortID, d.Timeout(schema.TimeoutCreate), powerinstanceid, networkname)
if err != nil {
return err
}
Expand All @@ -95,7 +99,28 @@ func resourceIBMPINetworkPortAttachCreate(d *schema.ResourceData, meta interface
}

func resourceIBMPINetworkPortAttachRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("Calling ther NetworkAttach Read code")
log.Printf("Calling ther Network Port Attach Read code")
sess, err := meta.(ClientSession).IBMPISession()

if err != nil {
fmt.Printf("failed to get a session from the IBM Cloud Service %v", err)
}

parts, err := idParts(d.Id())
if err != nil {
return err
}

powerinstanceid := parts[0]
powernetworkname := d.Get(helpers.PINetworkName).(string)
networkC := st.NewIBMPINetworkClient(sess, powerinstanceid)
networkdata, err := networkC.GetPort(powernetworkname, powerinstanceid, parts[1], getTimeOut)

d.Set("ipaddress", networkdata.IPAddress)
d.Set("macaddress", networkdata.MacAddress)
d.Set("status", networkdata.Status)
d.Set("portid", networkdata.PortID)
d.Set("pvminstance", networkdata.PvmInstance.Href)

return nil
}
Expand All @@ -108,32 +133,44 @@ func resourceIBMPINetworkPortAttachUpdate(d *schema.ResourceData, meta interface
func resourceIBMPINetworkPortAttachDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("Detaching the network port from the Instance ")

/* log.Printf("Calling the network delete functions. ")
sess, err := meta.(ClientSession).IBMPISession()
if err != nil {
return err
}
parts, err := idParts(d.Id())
powernetworkname := d.Get(helpers.PINetworkName).(string)
if err != nil {
return err
}
powerinstanceid := parts[0]
portid := parts[1]
client := st.NewIBMPINetworkClient(sess, powerinstanceid)
sess, err := meta.(ClientSession).IBMPISession()

if err != nil {
fmt.Printf("failed to get a session from the IBM Cloud Service %v", err)

}
parts, err := idParts(d.Id())
if err != nil {
return err
}

network, err := client.DetachPort(powerinstanceid, powernetworkname, portid, deleteTimeOut)*/
powerinstanceid := parts[0]
powernetworkname := d.Get(helpers.PINetworkName).(string)
portid := d.Get("port_id").(string)

client := st.NewIBMPINetworkClient(sess, powerinstanceid)
log.Printf("Calling the network delete functions. ")
network, err := client.DetachPort(powerinstanceid, powernetworkname, portid, deleteTimeOut)
if err != nil {
return err
}

log.Printf("Printing the networkresponse %+v", &network)

//log.Printf("Printing the networkresponse %s", network.Status)

d.SetId("")
return nil

}

func isWaitForIBMPINetworkPortAvailable(client *st.IBMPINetworkClient, id string, timeout time.Duration, powerinstanceid, networkname string) (interface{}, error) {
func isWaitForIBMPINetworkPortAttachAvailable(client *st.IBMPINetworkClient, id string, timeout time.Duration, powerinstanceid, networkname string) (interface{}, error) {
log.Printf("Waiting for Power Network (%s) that was created for Network Zone (%s) to be available.", id, networkname)

stateConf := &resource.StateChangeConf{
Pending: []string{"retry", helpers.PINetworkProvisioning},
Target: []string{"DOWN"},
Refresh: isIBMPINetworkPortRefreshFunc(client, id, powerinstanceid, networkname),
Target: []string{"ACTIVE"},
Refresh: isIBMPINetworkPortAttachRefreshFunc(client, id, powerinstanceid, networkname),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 10 * time.Minute,
Expand All @@ -142,7 +179,7 @@ func isWaitForIBMPINetworkPortAvailable(client *st.IBMPINetworkClient, id string
return stateConf.WaitForState()
}

func isIBMPINetworkPortRefreshFunc(client *st.IBMPINetworkClient, id, powerinstanceid, networkname string) resource.StateRefreshFunc {
func isIBMPINetworkPortAttachRefreshFunc(client *st.IBMPINetworkClient, id, powerinstanceid, networkname string) resource.StateRefreshFunc {

log.Printf("Calling the IsIBMPINetwork Refresh Function....with the following id (%s) for network port and following id (%s) for network name and waiting for network to be READY", id, networkname)
return func() (interface{}, string, error) {
Expand All @@ -151,10 +188,10 @@ func isIBMPINetworkPortRefreshFunc(client *st.IBMPINetworkClient, id, powerinsta
return nil, "", err
}

if &network.PortID != nil {
if &network.PortID != nil && &network.PvmInstance.PvmInstanceID != nil {
//if network.State == "available" {
log.Printf(" The port has been created with the following ip address")
return network, "DOWN", nil
log.Printf(" The port has been created with the following ip address and attached to an instance ")
return network, "ACTIVE", nil
}

return network, helpers.PINetworkProvisioning, nil
Expand Down
Loading

0 comments on commit bf17cba

Please sign in to comment.