Skip to content

Commit

Permalink
Refactor Cloud Connection Network Attach (IBM-Cloud#5781)
Browse files Browse the repository at this point in the history
  • Loading branch information
ismirlia committed Nov 18, 2024
1 parent 4a1f982 commit a070930
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@ import (
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

st "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
)

const (
PICloudConnectionNetworkId = "pi_network_id"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func ResourceIBMPICloudConnectionNetworkAttach() *schema.Resource {
Expand All @@ -35,24 +30,27 @@ func ResourceIBMPICloudConnectionNetworkAttach() *schema.Resource {
},

Schema: map[string]*schema.Schema{
// Required Attributes
helpers.PICloudInstanceId: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "PI cloud instance ID",
// Arguments
Arg_CloudConnectionID: {
Description: "Cloud Connection ID",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
helpers.PICloudConnectionId: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Cloud Connection ID",
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
PICloudConnectionNetworkId: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Network ID to attach to this cloud connection",
Arg_NetworkID: {
Description: "Network ID to attach to this cloud connection",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
},
}
Expand All @@ -64,12 +62,12 @@ func resourceIBMPICloudConnectionNetworkAttachCreate(ctx context.Context, d *sch
return diag.FromErr(err)
}

cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
cloudConnectionID := d.Get(helpers.PICloudConnectionId).(string)
networkID := d.Get(PICloudConnectionNetworkId).(string)
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
cloudConnectionID := d.Get(Arg_CloudConnectionID).(string)
networkID := d.Get(Arg_NetworkID).(string)

client := st.NewIBMPICloudConnectionClient(ctx, sess, cloudInstanceID)
jobClient := st.NewIBMPIJobClient(ctx, sess, cloudInstanceID)
client := instance.NewIBMPICloudConnectionClient(ctx, sess, cloudInstanceID)
jobClient := instance.NewIBMPIJobClient(ctx, sess, cloudInstanceID)

_, jobReference, err := client.AddNetwork(cloudConnectionID, networkID)
if err != nil {
Expand Down Expand Up @@ -97,9 +95,9 @@ func resourceIBMPICloudConnectionNetworkAttachRead(ctx context.Context, d *schem
cloudConnectionID := parts[1]
networkID := parts[2]

d.Set(helpers.PICloudInstanceId, cloudInstanceID)
d.Set(helpers.PICloudConnectionId, cloudConnectionID)
d.Set(PICloudConnectionNetworkId, networkID)
d.Set(Arg_CloudInstanceID, cloudInstanceID)
d.Set(Arg_CloudConnectionID, cloudConnectionID)
d.Set(Arg_NetworkID, networkID)

return nil
}
Expand All @@ -119,16 +117,16 @@ func resourceIBMPICloudConnectionNetworkAttachDelete(ctx context.Context, d *sch
cloudConnectionID := parts[1]
networkID := parts[2]

client := st.NewIBMPICloudConnectionClient(ctx, sess, cloudInstanceID)
jobClient := st.NewIBMPIJobClient(ctx, sess, cloudInstanceID)
client := instance.NewIBMPICloudConnectionClient(ctx, sess, cloudInstanceID)
jobClient := instance.NewIBMPIJobClient(ctx, sess, cloudInstanceID)

_, jobReference, err := client.DeleteNetwork(cloudConnectionID, networkID)
if err != nil {
log.Printf("[DEBUG] detach network from cloud connection failed %v", err)
return diag.FromErr(err)
}
if jobReference != nil {
_, err = waitForIBMPIJobCompleted(ctx, jobClient, *jobReference.ID, d.Timeout(schema.TimeoutUpdate))
_, err = waitForIBMPIJobCompleted(ctx, jobClient, *jobReference.ID, d.Timeout(schema.TimeoutDelete))
if err != nil {
return diag.FromErr(err)
}
Expand Down
34 changes: 16 additions & 18 deletions website/docs/r/pi_cloud_connection_network_attach.html.markdown
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---

subcategory: "Power Systems"
layout: "ibm"
page_title: "IBM: pi_cloud_connection_network_attach"
Expand All @@ -9,28 +8,28 @@ description: |-

# ibm_pi_cloud_connection_network_attach

Attach, detach Network to a Cloud Connection for a Power Systems Virtual Server. For more information, about IBM power virtual server cloud, see [getting started with IBM Power Systems Virtual Servers](https://cloud.ibm.com/docs/power-iaas?topic=power-iaas-getting-started).
Attach, and detach a network to a cloud connection for a Power Systems Virtual Server. For more information, about IBM power virtual server cloud, see [getting started with IBM Power Systems Virtual Servers](https://cloud.ibm.com/docs/power-iaas?topic=power-iaas-getting-started).

## Example usage

The following example enables you attach a network to a cloud connection:

```terraform
resource "ibm_pi_cloud_connection_network_attach" "example" {
pi_cloud_instance_id = "<value of the service instance id>"
pi_cloud_connection_id = "<value of the cloud connection id>"
pi_network_id = "<value of the network id>"
pi_cloud_instance_id = "<value of the service instance id>"
pi_network_id = "<value of the network id>"
}
```

**Note**
### Notes

* Please find [supported Regions](https://cloud.ibm.com/apidocs/power-cloud#endpoint) for endpoints.
* If a Power cloud instance is provisioned at `lon04`, The provider level attributes should be as follows:
* `region` - `lon`
* `zone` - `lon04`
- Please find [supported Regions](https://cloud.ibm.com/apidocs/power-cloud#endpoint) for endpoints.
- If a Power cloud instance is provisioned at `lon04`, The provider level attributes should be as follows:
- `region` - `lon`
- `zone` - `lon04`

Example usage:
Example usage:

```terraform
provider "ibm" {
Expand All @@ -43,30 +42,29 @@ resource "ibm_pi_cloud_connection_network_attach" "example" {

The `ibm_pi_cloud_connection_network_attach` provides the following [timeouts](https://www.terraform.io/docs/language/resources/syntax.html) configuration options:

- **Create** The attach of network to the cloud connection is considered failed if no response is received for 30 minutes.
- **Delete** The detach of network from the cloud connection is considered failed if no response is received for 30 minutes.
- **create** - (Default 30 minutes) Used for attaching a network from a cloud connection.
- **delete** - (Default 30 minutes) Used for detaching a network from a cloud connection.

## Argument reference

Review the argument references that you can specify for your resource.
Review the argument references that you can specify for your resource.

- `pi_cloud_instance_id` - (Required, String) The GUID of the service instance associated with an account.
- `pi_cloud_connection_id` - (Required, String) The Cloud Connection ID.
- `pi_cloud_instance_id` - (Required, String) The GUID of the service instance associated with an account.
- `pi_network_id` - (Required, String) The Network ID to attach to this cloud connection.


## Attribute reference

In addition to all argument reference list, you can access the following attribute reference after your resource is created.

- `id` - (String) The unique identifier of cloud connection network attachment.
- `id` - (String) The unique identifier of cloud connection network attachment. The ID is composed of `<pi_cloud_instance_id>/<pi_cloud_connection_id>/<pi_network_id>`.

## Import

The `ibm_pi_cloud_connection_network_attach` can be imported by using `pi_cloud_instance_id`, `pi_cloud_connection_id` and `pi_network_id`.

**Example**
### Example

```sh
$ terraform import ibm_pi_cloud_connection_network_attach.example d7bec597-4726-451f-8a63-e62e6f19c32c/cea6651a-bc0a-4438-9f8a-a0770bbf3ebb/4726d7be-c597-4438-9f8a-cea6651abc0a
terraform import ibm_pi_cloud_connection_network_attach.example d7bec597-4726-451f-8a63-e62e6f19c32c/cea6651a-bc0a-4438-9f8a-a0770bbf3ebb/4726d7be-c597-4438-9f8a-cea6651abc0a
```

0 comments on commit a070930

Please sign in to comment.