Skip to content

Commit

Permalink
[NEW]: Implement opentelekomcloud_config for `rancher2_machine_conf…
Browse files Browse the repository at this point in the history
…ig_v2` (#5)

[NEW]: Implement `opentelekomcloud_config` for `rancher2_machine_config_v2`

Summary of the Pull Request
Added support for OpenTelekomCloud for rancher2_machine_config_v2 resource.
PR Checklist

 Refers to: #3
 Tests added/passed.
 Documentation updated.
 Schema updated.

Acceptance tests setup
Following minimal env vars are needed to run tests:
RANCHER_INSECURE=false;
RANCHER_TOKEN_KEY=token-XXXXXXXXXX;
RANCHER_URL=https://rancher-test.XXXXXX.in;
TF_ACC=1

Acceptance Steps Performed
2024/04/02 14:42:11 Getting from  https://rancher-test.XXXXXXXin/ping
2024/04/02 14:42:11 Time to get req:  219  ms
=== RUN   TestAccRancher2MachineConfigV2Basic
--- PASS: TestAccRancher2MachineConfigV2Basic (7.68s)
PASS
Process finished with the exit code 0

Reviewed-by: Anton Sidelnikov
  • Loading branch information
artem-lifshits authored Apr 3, 2024
1 parent 76d0f3b commit 6823dea
Show file tree
Hide file tree
Showing 8 changed files with 789 additions and 10 deletions.
49 changes: 48 additions & 1 deletion docs/resources/machine_config_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ page_title: "rancher2_machine_config_v2 Resource"

Provides a Rancher v2 Machine config v2 resource. This can be used to create Machine Config v2 for Rancher v2 and retrieve their information. This resource is available from Rancher v2.6.0 and above.

The supported cloud providers includes `amazonec2`, `azure`, `digitalocean`, `harvester`, `linode`, `openstack`, and `vsphere`.
The supported cloud providers includes `amazonec2`, `azure`, `digitalocean`, `harvester`, `linode`, `openstack`, `opentelekomcloud` and `vsphere`.

## Example Usage

Expand Down Expand Up @@ -292,6 +292,53 @@ The following attributes are exported:
> **Note:**: `Required++` denotes that either the _name or _id is required unless `application_credential_id` is defined.
> **Note for OpenStack users:**: `keypair_name` is required to be in the schema even if there are no references in rancher itself
### `opentelekomcloud_config`

#### Arguments

* `auth_url` - (Required) OpenTelekomCloud authentication URL (string)
* `access_key` - (Optional/Sensitive) OpenTelekomCloud access key (string)
* `secret_key` - (Optional/Sensitive) OpenTelekomCloud secret key (string)
* `availability_zone` - (Required) OpenTelekomCloud availability zone (string)
* `ca_cert` - (Optional) CA certificate bundle to verify against (string)
* `domain_id` - (Required+) OpenTelekomCloud domain ID. Conflicts with `domain_name` (string)
* `domain_name` - (Required+) OpenTelekomCloud domain name. Conflicts with `domain_id` (string)
* `eip` - (Optional) OpenTelekomCloud elastic ip address (string)
* `eip_type` - (Optional) OpenTelekomCloud elastic ip type. Default `5_bgp` (string)
* `endpoint_type` - (Optional) OpenTelekomCloud endpoint type. Default `public` (string)
* `flavor_id` - (Optional) OpenTelekomCloud flavor id to use for the instance (string)
* `flavor_name` - (Optional) OpenTelekomCloud flavor name to use for the instance. Default `s3.xlarge.2` (string)
* `bandwidth_size` - (Optional) OpenTelekomCloud elastic IP bandwidth size. Default `100` (string)
* `bandwidth_type` - (Optional) OpenTelekomCloud elastic IP bandwidth type. Default `PER` (string)
* `image_id` - (Required+) OpenTelekomCloud image id to use for the instance (string)
* `image_name` - (Required+) OpenTelekomCloud image name to use for the instance (string)
* `ip_version` - (Optional) OpenTelekomCloud version of IP address assigned for the machine. Default `4` (string)
* `keypair_name` - (Optional) OpenTelekomCloud keypair to use to SSH to the instance (string)
* `password` - (Optional/Sensitive) OpenTelekomCloud password (string)
* `private_key_file` - (Optional) Private key content to use for SSH (string)
* `project_id` - (Required+) OpenTelekomCloud project id. Conflicts with `project_name` (string)
* `project_name` - (Required+) OpenTelekomCloud project name. Conflicts with `project_id` (string)
* `region` - (Required) OpenTelekomCloud region name (string)
* `root_volume_size` - (Optional) OpenTelekomCloud volume size (GiB). Default `40` (string)
* `root_volume_type` - (Optional) OpenTelekomCloud volume type. Default `SSD` (string)
* `security_groups` - (Optional) OpenTelekomCloud list of security groups for the machine (list)
* `server_group` - (Optional) OpenTelekomCloud server group where server will be created (string)
* `server_group_id` - (Optional) OpenTelekomCloud server group id where server will be created (string)
* `skip_default_sg` - (Optional) Parameter to skip default security group creation (bool)
* `skip_eip` - (Optional) Parameter to skip eip creation and set machine IP to instance local IP (bool)
* `ssh_port` - (Optional) OpenTelekomCloud SSH port * Default `22` (string)
* `ssh_user` - (Optional) OpenTelekomCloud SSH user * Default: `ubuntu` (string)
* `subnet_id` - (Required++) OpenTelekomCloud subnet ID of the machine. Conflicts with `subnet_name` (string)
* `subnet_name` - (Required++) OpenTelekomCloud subnet name of the machine. Conflicts with `subnet_id` (string)
* `token` - (Optional/Sensitive) OpenTelekomCloud authentication token (string)
* `tags` - (Optional) OpenTelekomCloud tags (e.g. key1.value1,key2.value2,key3) (string)
* `user_data_file` - (Optional) File containing an OpenTelekomCloud userdata script (string)
* `user_data_raw` - (Optional) Contents of the OpenTelekomCloud userdata as a string (string)
* `username` - (Optional) OpenTelekomCloud username (string)
* `vpc_id` - (Required+) OpenTelekomCloud virtual private cloud id. Conflicts with `vpc_name` (string)
* `vpc_name` - (Required+) OpenTelekomCloud virtual private cloud name. Conflicts with `vpc_id` (string)
> **Note:**: `Required+` denotes that either the _name or _id is required, but you cannot use both.
### `vsphere_config`

#### Arguments
Expand Down
24 changes: 24 additions & 0 deletions rancher2/resource_rancher2_machine_config_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ func createMachineConfigV2(c *Config, obj *MachineConfigV2) (*MachineConfigV2, e
out.ID = resp.ID
out.TypeMeta = resp.TypeMeta
out.ObjectMeta = resp.ObjectMeta
case machineConfigV2OpentelekomcloudKind:
resp := &MachineConfigV2Opentelekomcloud{}
err = c.createObjectV2(rancher2DefaultLocalClusterID, machineConfigV2OpentelekomcloudAPIType, obj.OpentelekomcloudConfig, resp)
out.OpentelekomcloudConfig = resp
out.ID = resp.ID
out.TypeMeta = resp.TypeMeta
out.ObjectMeta = resp.ObjectMeta
case machineConfigV2VmwarevsphereKind:
resp := &MachineConfigV2Vmwarevsphere{}
err = c.createObjectV2(rancher2DefaultLocalClusterID, machineConfigV2VmwarevsphereAPIType, obj.VmwarevsphereConfig, resp)
Expand Down Expand Up @@ -325,6 +332,16 @@ func getMachineConfigV2ByID(c *Config, id, kind string) (*MachineConfigV2, error
out.Type = resp.Type
out.TypeMeta = resp.TypeMeta
out.ObjectMeta = resp.ObjectMeta
case machineConfigV2OpentelekomcloudKind:
resp := &MachineConfigV2Opentelekomcloud{}
err = c.getObjectV2ByID(rancher2DefaultLocalClusterID, id, machineConfigV2OpentelekomcloudAPIType, resp)
out.OpentelekomcloudConfig = resp
out.ID = resp.ID
out.Links = resp.Links
out.Actions = resp.Actions
out.Type = resp.Type
out.TypeMeta = resp.TypeMeta
out.ObjectMeta = resp.ObjectMeta
case machineConfigV2VmwarevsphereKind:
resp := &MachineConfigV2Vmwarevsphere{}
err = c.getObjectV2ByID(rancher2DefaultLocalClusterID, id, machineConfigV2VmwarevsphereAPIType, resp)
Expand Down Expand Up @@ -400,6 +417,13 @@ func updateMachineConfigV2(c *Config, obj *MachineConfigV2) (*MachineConfigV2, e
out.ID = resp.ID
out.TypeMeta = resp.TypeMeta
out.ObjectMeta = resp.ObjectMeta
case machineConfigV2OpentelekomcloudKind:
resp := &MachineConfigV2Opentelekomcloud{}
err = c.updateObjectV2(rancher2DefaultLocalClusterID, obj.ID, machineConfigV2OpentelekomcloudAPIType, obj.OpentelekomcloudConfig, resp)
out.OpentelekomcloudConfig = resp
out.ID = resp.ID
out.TypeMeta = resp.TypeMeta
out.ObjectMeta = resp.ObjectMeta
case machineConfigV2VmwarevsphereKind:
resp := &MachineConfigV2Vmwarevsphere{}
err = c.updateObjectV2(rancher2DefaultLocalClusterID, obj.ID, machineConfigV2VmwarevsphereAPIType, obj.VmwarevsphereConfig, resp)
Expand Down
138 changes: 138 additions & 0 deletions rancher2/resource_rancher2_machine_config_v2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package rancher2

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"testing"
)

const (
testAccRancher2MachineConfigV2 = "rancher2_machine_config_v2"
)

var (
testAccRancher2MachineConfigV2Basic string
testAccRancher2MachineConfigV2Update string
)

func init() {
testAccRancher2MachineConfigV2Basic = `
resource "` + testAccRancher2MachineConfigV2 + `" "foo" {
generate_name = "test-foo"
opentelekomcloud_config {
auth_url = "https://iam.eu-de.otc.t-systems.com/v3"
availability_zone = "eu-de-01"
username = "test-user"
password = "dummy-password"
region = "eu-de"
project_name = "eu-de"
domain_name = "OTC-EU-DE-00000000001-TEST-DOMAIN"
flavor_name = "s3.medium.1"
subnet_name = "subnet-test"
vpc_name = "vpc-test"
tags = "key1.value1,key2.value2,key3"
security_groups = ["default", "sg-test"]
}
}`
testAccRancher2MachineConfigV2Update = `
resource "` + testAccRancher2MachineConfigV2 + `" "foo" {
generate_name = "test-foo"
opentelekomcloud_config {
auth_url = "https://iam.eu-de.otc.t-systems.com/v3"
availability_zone = "eu-de-01"
username = "test-user"
password = "dummy-password-2"
region = "eu-nl"
project_name = "eu-de_main"
domain_name = "OTC-EU-DE-00000000001-TEST-DOMAIN"
flavor_name = "s3x.2.medium.1"
subnet_name = "subnet-prod"
vpc_name = "vpc-test"
security_groups = ["default"]
}
}`
}

func TestAccRancher2MachineConfigV2Basic(t *testing.T) {
rsName := testAccRancher2MachineConfigV2 + ".foo"
var machineConfig *MachineConfigV2Opentelekomcloud
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckRancher2MachineConfigV2Destroy,
Steps: []resource.TestStep{
{
Config: testAccRancher2MachineConfigV2Basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancher2MachineConfigV2Exists(rsName, machineConfig),
resource.TestCheckResourceAttrSet(rsName, "name"),
resource.TestCheckResourceAttr(rsName, "opentelekomcloud_config.0.region", "eu-de"),
resource.TestCheckResourceAttr(rsName, "opentelekomcloud_config.0.project_name", "eu-de"),
resource.TestCheckResourceAttr(rsName, "opentelekomcloud_config.0.flavor_name", "s3.medium.1"),
resource.TestCheckResourceAttr(rsName, "opentelekomcloud_config.0.subnet_name", "subnet-test"),
resource.TestCheckResourceAttr(rsName, "opentelekomcloud_config.0.security_groups.1", "sg-test"),
),
},
{
Config: testAccRancher2MachineConfigV2Update,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancher2MachineConfigV2Exists(rsName, machineConfig),
resource.TestCheckResourceAttrSet(rsName, "name"),
resource.TestCheckResourceAttr(rsName, "opentelekomcloud_config.0.region", "eu-nl"),
resource.TestCheckResourceAttr(rsName, "opentelekomcloud_config.0.project_name", "eu-de_main"),
resource.TestCheckResourceAttr(rsName, "opentelekomcloud_config.0.flavor_name", "s3x.2.medium.1"),
resource.TestCheckResourceAttr(rsName, "opentelekomcloud_config.0.subnet_name", "subnet-prod"),
resource.TestCheckResourceAttr(rsName, "opentelekomcloud_config.0.security_groups.0", "default"),
),
},
},
})
}

func testAccCheckRancher2MachineConfigV2Exists(n string, machineConfig *MachineConfigV2Opentelekomcloud) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No Machine Config ID is set")
}

client, _ := testAccProvider.Meta().(*Config)

machineConfig = &MachineConfigV2Opentelekomcloud{}

err := client.getObjectV2ByID("local", rs.Primary.ID, machineConfigV2OpentelekomcloudAPIType, machineConfig)
if err != nil {
if IsNotFound(err) {
return fmt.Errorf("Machine Config V2 not found")
}
return err
}

return nil
}
}

func testAccCheckRancher2MachineConfigV2Destroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != testAccRancher2MachineConfigV2 {
continue
}
client, _ := testAccProvider.Meta().(*Config)

resp := &MachineConfigV2Opentelekomcloud{}

err := client.getObjectV2ByID("local", rs.Primary.ID, machineConfigV2OpentelekomcloudAPIType, resp)
if err != nil {
if IsNotFound(err) {
return nil
}
return err
}
return fmt.Errorf("Machine Config V2 still exists")
}
return nil
}
10 changes: 10 additions & 0 deletions rancher2/schema_machine_config_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var allMachineDriverConfigFields = []string{
"harvester_config",
"linode_config",
"openstack_config",
"opentelekomcloud_config",
"vsphere_config",
}

Expand Down Expand Up @@ -92,6 +93,15 @@ func machineConfigV2Fields() map[string]*schema.Schema {
Schema: machineConfigV2OpenstackFields(),
},
},
"opentelekomcloud_config": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: getConflicts(allMachineDriverConfigFields, "opentelekomcloud_config"),
Elem: &schema.Resource{
Schema: machineConfigV2OpentelekomcloudFields(),
},
},
"vsphere_config": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down
Loading

0 comments on commit 6823dea

Please sign in to comment.