-
Notifications
You must be signed in to change notification settings - Fork 676
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resource for cloud connection network attach
Signed-off-by: Yussuf Shaikh <[email protected]>
- Loading branch information
Showing
4 changed files
with
291 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
ibm/service/power/resource_ibm_pi_cloud_connection_network_attach.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright IBM Corp. 2022 All Rights Reserved. | ||
// Licensed under the Mozilla Public License v2.0 | ||
|
||
package power | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"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/terraform-provider-ibm/ibm/conns" | ||
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" | ||
) | ||
|
||
const ( | ||
PICloudConnectionNetworkId = "pi_network_id" | ||
) | ||
|
||
func ResourceIBMPICloudConnectionNetworkAttach() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceIBMPICloudConnectionNetworkAttachCreate, | ||
ReadContext: resourceIBMPICloudConnectionNetworkAttachRead, | ||
DeleteContext: resourceIBMPICloudConnectionNetworkAttachDelete, | ||
Importer: &schema.ResourceImporter{}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(30 * time.Minute), | ||
Delete: schema.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
// Required Attributes | ||
helpers.PICloudInstanceId: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "PI cloud instance ID", | ||
}, | ||
helpers.PICloudConnectionId: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "Cloud Connection ID", | ||
}, | ||
PICloudConnectionNetworkId: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "Network ID to attach to this cloud connection", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceIBMPICloudConnectionNetworkAttachCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
sess, err := meta.(conns.ClientSession).IBMPISession() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string) | ||
cloudConnectionID := d.Get(helpers.PICloudConnectionId).(string) | ||
networkID := d.Get(PICloudConnectionNetworkId).(string) | ||
|
||
client := st.NewIBMPICloudConnectionClient(ctx, sess, cloudInstanceID) | ||
jobClient := st.NewIBMPIJobClient(ctx, sess, cloudInstanceID) | ||
|
||
_, jobReference, err := client.AddNetwork(cloudConnectionID, networkID) | ||
if err != nil { | ||
log.Printf("[ERROR] attach network to cloud connection failed %v", err) | ||
return diag.FromErr(err) | ||
} | ||
d.SetId(fmt.Sprintf("%s/%s/%s", cloudInstanceID, cloudConnectionID, networkID)) | ||
if jobReference != nil { | ||
_, err = waitForIBMPIJobCompleted(ctx, jobClient, *jobReference.ID, d.Timeout(schema.TimeoutCreate)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
} | ||
|
||
return resourceIBMPICloudConnectionNetworkAttachRead(ctx, d, meta) | ||
} | ||
|
||
func resourceIBMPICloudConnectionNetworkAttachRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
parts, err := flex.IdParts(d.Id()) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
cloudInstanceID := parts[0] | ||
cloudConnectionID := parts[1] | ||
networkID := parts[2] | ||
|
||
d.Set(helpers.PICloudInstanceId, cloudInstanceID) | ||
d.Set(helpers.PICloudConnectionId, cloudConnectionID) | ||
d.Set(PICloudConnectionNetworkId, networkID) | ||
|
||
return nil | ||
} | ||
|
||
func resourceIBMPICloudConnectionNetworkAttachDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
sess, err := meta.(conns.ClientSession).IBMPISession() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
parts, err := flex.IdParts(d.Id()) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
cloudInstanceID := parts[0] | ||
cloudConnectionID := parts[1] | ||
networkID := parts[2] | ||
|
||
client := st.NewIBMPICloudConnectionClient(ctx, sess, cloudInstanceID) | ||
jobClient := st.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)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} |
60 changes: 60 additions & 0 deletions
60
ibm/service/power/resource_ibm_pi_cloud_connection_network_attach_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright IBM Corp. 2022 All Rights Reserved. | ||
// Licensed under the Mozilla Public License v2.0 | ||
|
||
package power_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccIBMPICloudConnectionNetworkAttachBasic(t *testing.T) { | ||
name := fmt.Sprintf("tf-ccnet-%d", acctest.RandIntRange(10, 100)) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { acc.TestAccPreCheck(t) }, | ||
Providers: acc.TestAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckIBMPICloudConnectionNetworkAttachConfig(name), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckIBMPICloudConnectionExists("ibm_pi_cloud_connection.cloud_connection"), | ||
resource.TestCheckResourceAttr("ibm_pi_cloud_connection.cloud_connection", | ||
"pi_cloud_connection_name", name), | ||
resource.TestCheckResourceAttr("data.ibm_pi_cloud_connection.cloud_connection", | ||
"networks.#", "1"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckIBMPICloudConnectionNetworkAttachConfig(name string) string { | ||
return fmt.Sprintf(` | ||
resource "ibm_pi_cloud_connection" "cloud_connection" { | ||
pi_cloud_instance_id = "%[1]s" | ||
pi_cloud_connection_name = "%[2]s" | ||
pi_cloud_connection_speed = 100 | ||
} | ||
resource "ibm_pi_network" "network1" { | ||
pi_cloud_instance_id = "%[1]s" | ||
pi_network_name = "%[2]s" | ||
pi_network_type = "vlan" | ||
pi_cidr = "192.152.61.0/24" | ||
} | ||
resource "ibm_pi_cloud_connection_network_attach" "example" { | ||
pi_cloud_instance_id = "%[1]s" | ||
pi_cloud_connection_id = ibm_pi_cloud_connection.cloud_connection.cloud_connection_id | ||
pi_network_id = ibm_pi_network.network1.network_id | ||
} | ||
data "ibm_pi_cloud_connection" "cloud_connection" { | ||
depends_on = [ibm_pi_cloud_connection_network_attach.example] | ||
pi_cloud_instance_id = "%[1]s" | ||
pi_cloud_connection_name = "%[2]s" | ||
} | ||
`, acc.Pi_cloud_instance_id, name) | ||
} |
72 changes: 72 additions & 0 deletions
72
website/docs/r/pi_cloud_connection_network_attach.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
|
||
subcategory: "Power Systems" | ||
layout: "ibm" | ||
page_title: "IBM: pi_cloud_connection_network_attach" | ||
description: |- | ||
Manages IBM Cloud Connection Network attachment in the Power Virtual Server cloud. | ||
--- | ||
|
||
# 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). | ||
|
||
## 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>" | ||
} | ||
``` | ||
|
||
**Note** | ||
|
||
* 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: | ||
|
||
```terraform | ||
provider "ibm" { | ||
region = "lon" | ||
zone = "lon04" | ||
} | ||
``` | ||
|
||
## Timeouts | ||
|
||
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. | ||
|
||
## Argument reference | ||
|
||
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_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. | ||
|
||
## 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** | ||
|
||
```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 | ||
``` |