Skip to content

Commit

Permalink
openstack_billing_tenant cloud spec field for aws and azure
Browse files Browse the repository at this point in the history
  • Loading branch information
furkhat committed Jun 29, 2021
1 parent 8ffb830 commit a6c445f
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 70 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/hashicorp/go-version v1.2.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.5.0
github.com/mitchellh/go-homedir v1.1.0
github.com/syseleven/go-metakube v0.0.0-20210416091829-b6a6fb1490a6
github.com/syseleven/go-metakube v0.0.0-20210624055517-a73033719ddf
go.uber.org/zap v1.16.0
golang.org/x/mod v0.3.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/syseleven/go-metakube v0.0.0-20210416091829-b6a6fb1490a6 h1:/bXn1zDkHppCEWW4z38auOMK/0st3YCT5EVzuRffzR0=
github.com/syseleven/go-metakube v0.0.0-20210416091829-b6a6fb1490a6/go.mod h1:4AYa17U1yL9SFYSC5VF7rH6Jr4tMYrFSboduGQhrCx8=
github.com/syseleven/go-metakube v0.0.0-20210624055517-a73033719ddf h1:IF8+oNGTwwp3d3LpLxgEJGrrLGF0jeIS6tkMgMnVFnw=
github.com/syseleven/go-metakube v0.0.0-20210624055517-a73033719ddf/go.mod h1:4AYa17U1yL9SFYSC5VF7rH6Jr4tMYrFSboduGQhrCx8=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
Expand Down
36 changes: 19 additions & 17 deletions metakube/resource_metakube_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,29 +455,31 @@ func readClusterPreserveValues(d *schema.ResourceData) clusterPreserveValues {
var azure *models.AzureCloudSpec
if _, ok := d.GetOk(key("azure.0")); ok {
azure = &models.AzureCloudSpec{
AvailabilitySet: d.Get(key("azure.0.availability_set")).(string),
ClientID: d.Get(key("azure.0.client_id")).(string),
ClientSecret: d.Get(key("azure.0.client_secret")).(string),
SubscriptionID: d.Get(key("azure.0.subscription_id")).(string),
TenantID: d.Get(key("azure.0.tenant_id")).(string),
ResourceGroup: d.Get(key("azure.0.resource_group")).(string),
RouteTableName: d.Get(key("azure.0.route_table")).(string),
SecurityGroup: d.Get(key("azure.0.security_group")).(string),
SubnetName: d.Get(key("azure.0.subnet")).(string),
VNetName: d.Get(key("azure.0.vnet")).(string),
AvailabilitySet: d.Get(key("azure.0.availability_set")).(string),
ClientID: d.Get(key("azure.0.client_id")).(string),
ClientSecret: d.Get(key("azure.0.client_secret")).(string),
SubscriptionID: d.Get(key("azure.0.subscription_id")).(string),
TenantID: d.Get(key("azure.0.tenant_id")).(string),
ResourceGroup: d.Get(key("azure.0.resource_group")).(string),
RouteTableName: d.Get(key("azure.0.route_table")).(string),
SecurityGroup: d.Get(key("azure.0.security_group")).(string),
SubnetName: d.Get(key("azure.0.subnet")).(string),
VNetName: d.Get(key("azure.0.vnet")).(string),
OpenstackBillingTenant: d.Get(key("azure.0.openstack_billing_tenant")).(string),
}
}

var aws *models.AWSCloudSpec
if _, ok := d.GetOk(key("aws.0")); ok {
aws = &models.AWSCloudSpec{
AccessKeyID: d.Get(key("aws.0.access_key_id")).(string),
SecretAccessKey: d.Get(key("aws.0.secret_access_key")).(string),
VPCID: d.Get(key("aws.0.vpc_id")).(string),
SecurityGroupID: d.Get(key("aws.0.security_group_id")).(string),
RouteTableID: d.Get(key("aws.0.route_table_id")).(string),
InstanceProfileName: d.Get(key("aws.0.instance_profile_name")).(string),
ControlPlaneRoleARN: d.Get(key("aws.0.role_arn")).(string),
AccessKeyID: d.Get(key("aws.0.access_key_id")).(string),
SecretAccessKey: d.Get(key("aws.0.secret_access_key")).(string),
VPCID: d.Get(key("aws.0.vpc_id")).(string),
SecurityGroupID: d.Get(key("aws.0.security_group_id")).(string),
RouteTableID: d.Get(key("aws.0.route_table_id")).(string),
InstanceProfileName: d.Get(key("aws.0.instance_profile_name")).(string),
ControlPlaneRoleARN: d.Get(key("aws.0.role_arn")).(string),
OpenstackBillingTenant: d.Get(key("aws.0.openstack_billing_tenant")).(string),
}
}

Expand Down
14 changes: 14 additions & 0 deletions metakube/resource_metakube_cluster_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ func metakubeResourceClusterAzureSpecFields() map[string]*schema.Schema {
Computed: true,
Optional: true,
},
"openstack_billing_tenant": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OS_PROJECT", ""),
ValidateFunc: validation.NoZeroValues,
Description: "Openstack tenant/project name for the account",
},
}
}

Expand Down Expand Up @@ -226,6 +233,13 @@ func metakubeResourceCluserAWSCloudSpecFields() map[string]*schema.Schema {
Optional: true,
Description: "The IAM role the control plane will use over assume-role",
},
"openstack_billing_tenant": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OS_PROJECT", ""),
ValidateFunc: validation.NoZeroValues,
Description: "Openstack tenant/project name for the account",
},
}
}

Expand Down
20 changes: 20 additions & 0 deletions metakube/resource_metakube_cluster_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ func flattenAWSCloudSpec(in *models.AWSCloudSpec) []interface{} {
att["role_arn"] = in.ControlPlaneRoleARN
}

if in.OpenstackBillingTenant != "" {
att["openstack_billing_tenant"] = in.OpenstackBillingTenant
}

if in.RouteTableID != "" {
att["route_table_id"] = in.RouteTableID
}
Expand Down Expand Up @@ -223,6 +227,10 @@ func flattenAzureSpec(in *models.AzureCloudSpec) []interface{} {
att["route_table"] = in.RouteTableName
}

if in.OpenstackBillingTenant != "" {
att["openstack_billing_tenant"] = in.OpenstackBillingTenant
}

if in.SecurityGroup != "" {
att["security_group"] = in.SecurityGroup
}
Expand Down Expand Up @@ -462,6 +470,12 @@ func expandAWSCloudSpec(p []interface{}) *models.AWSCloudSpec {
}
}

if v, ok := in["openstack_billing_tenant"]; ok {
if vv, ok := v.(string); ok && vv != "" {
obj.OpenstackBillingTenant = vv
}
}

if v, ok := in["route_table_id"]; ok {
if vv, ok := v.(string); ok && vv != "" {
obj.RouteTableID = vv
Expand Down Expand Up @@ -591,6 +605,12 @@ func expandAzureCloudSpec(p []interface{}) *models.AzureCloudSpec {
}
}

if v, ok := in["openstack_billing_tenant"]; ok {
if vv, ok := v.(string); ok && vv != "" {
obj.OpenstackBillingTenant = vv
}
}

if v, ok := in["security_group"]; ok {
if vv, ok := v.(string); ok && vv != "" {
obj.SecurityGroup = vv
Expand Down
98 changes: 52 additions & 46 deletions metakube/resource_metakube_cluster_structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,25 @@ func TestFlattenAWSCloudSpec(t *testing.T) {
}{
{
&models.AWSCloudSpec{
AccessKeyID: "AKIAIOSFODNN7EXAMPLE",
ControlPlaneRoleARN: "default",
InstanceProfileName: "default",
RouteTableID: "rtb-09ba434c1bEXAMPLE",
SecretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
SecurityGroupID: "sg-51530134",
VPCID: "e5e4b2ef2fe",
AccessKeyID: "AKIAIOSFODNN7EXAMPLE",
ControlPlaneRoleARN: "default",
InstanceProfileName: "default",
OpenstackBillingTenant: "foo",
RouteTableID: "rtb-09ba434c1bEXAMPLE",
SecretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
SecurityGroupID: "sg-51530134",
VPCID: "e5e4b2ef2fe",
},
[]interface{}{
map[string]interface{}{
"access_key_id": "AKIAIOSFODNN7EXAMPLE",
"role_arn": "default",
"instance_profile_name": "default",
"route_table_id": "rtb-09ba434c1bEXAMPLE",
"secret_access_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"security_group_id": "sg-51530134",
"vpc_id": "e5e4b2ef2fe",
"access_key_id": "AKIAIOSFODNN7EXAMPLE",
"role_arn": "default",
"openstack_billing_tenant": "foo",
"instance_profile_name": "default",
"route_table_id": "rtb-09ba434c1bEXAMPLE",
"secret_access_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"security_group_id": "sg-51530134",
"vpc_id": "e5e4b2ef2fe",
},
},
},
Expand Down Expand Up @@ -234,27 +236,29 @@ func TestFlattenAzureCloudSpec(t *testing.T) {
}{
{
&models.AzureCloudSpec{
ClientID: "ClientID",
ClientSecret: "ClientSecret",
SubscriptionID: "SubscriptionID",
TenantID: "TenantID",
ResourceGroup: "ResourceGroup",
RouteTableName: "RouteTableName",
SecurityGroup: "SecurityGroup",
SubnetName: "SubnetName",
VNetName: "VNetName",
ClientID: "ClientID",
ClientSecret: "ClientSecret",
SubscriptionID: "SubscriptionID",
TenantID: "TenantID",
ResourceGroup: "ResourceGroup",
RouteTableName: "RouteTableName",
SecurityGroup: "SecurityGroup",
SubnetName: "SubnetName",
VNetName: "VNetName",
OpenstackBillingTenant: "foo",
},
[]interface{}{
map[string]interface{}{
"client_id": "ClientID",
"client_secret": "ClientSecret",
"tenant_id": "TenantID",
"subscription_id": "SubscriptionID",
"resource_group": "ResourceGroup",
"route_table": "RouteTableName",
"security_group": "SecurityGroup",
"subnet": "SubnetName",
"vnet": "VNetName",
"client_id": "ClientID",
"client_secret": "ClientSecret",
"tenant_id": "TenantID",
"subscription_id": "SubscriptionID",
"resource_group": "ResourceGroup",
"route_table": "RouteTableName",
"security_group": "SecurityGroup",
"subnet": "SubnetName",
"vnet": "VNetName",
"openstack_billing_tenant": "foo",
},
},
},
Expand Down Expand Up @@ -484,23 +488,25 @@ func TestExpandAWSCloudSpec(t *testing.T) {
{
[]interface{}{
map[string]interface{}{
"access_key_id": "AKIAIOSFODNN7EXAMPLE",
"role_arn": "default",
"instance_profile_name": "default",
"route_table_id": "rtb-09ba434c1bEXAMPLE",
"secret_access_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"security_group_id": "sg-51530134",
"vpc_id": "e5e4b2ef2fe",
"access_key_id": "AKIAIOSFODNN7EXAMPLE",
"role_arn": "default",
"openstack_billing_tenant": "foo",
"instance_profile_name": "default",
"route_table_id": "rtb-09ba434c1bEXAMPLE",
"secret_access_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"security_group_id": "sg-51530134",
"vpc_id": "e5e4b2ef2fe",
},
},
&models.AWSCloudSpec{
AccessKeyID: "AKIAIOSFODNN7EXAMPLE",
ControlPlaneRoleARN: "default",
InstanceProfileName: "default",
RouteTableID: "rtb-09ba434c1bEXAMPLE",
SecretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
SecurityGroupID: "sg-51530134",
VPCID: "e5e4b2ef2fe",
AccessKeyID: "AKIAIOSFODNN7EXAMPLE",
ControlPlaneRoleARN: "default",
OpenstackBillingTenant: "foo",
InstanceProfileName: "default",
RouteTableID: "rtb-09ba434c1bEXAMPLE",
SecretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
SecurityGroupID: "sg-51530134",
VPCID: "e5e4b2ef2fe",
},
},
{
Expand Down
16 changes: 10 additions & 6 deletions metakube/resource_metakube_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,14 +579,15 @@ func TestAccMetakubeCluster_Azure_Basic(t *testing.T) {
subsID := os.Getenv(testEnvAzureSubscriptionID)
nodeDC := os.Getenv(testEnvAzureNodeDC)
k8sVersion := os.Getenv(testEnvK8sVersion)
billingTenant := os.Getenv(testEnvOpenstackTenant)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckForAzure(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMetaKubeClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckMetaKubeClusterAzureBasic(clusterName, clientID, clientSecret, tenantID, subsID, nodeDC, k8sVersion),
Config: testAccCheckMetaKubeClusterAzureBasic(clusterName, clientID, clientSecret, tenantID, subsID, nodeDC, billingTenant, k8sVersion),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckMetaKubeClusterExists(&cluster),
resource.TestCheckResourceAttr(resourceName, "spec.0.cloud.0.azure.0.client_id", clientID),
Expand All @@ -599,7 +600,7 @@ func TestAccMetakubeCluster_Azure_Basic(t *testing.T) {
})
}

func testAccCheckMetaKubeClusterAzureBasic(n, clientID, clientSecret, tenantID, subscID, nodeDC, k8sVersion string) string {
func testAccCheckMetaKubeClusterAzureBasic(n, clientID, clientSecret, tenantID, subscID, nodeDC, billingTenant, k8sVersion string) string {
return fmt.Sprintf(`
resource "metakube_project" "acctest_project" {
name = "%s"
Expand All @@ -618,10 +619,11 @@ func testAccCheckMetaKubeClusterAzureBasic(n, clientID, clientSecret, tenantID,
client_secret = "%s"
tenant_id = "%s"
subscription_id = "%s"
openstack_billing_tenant = "%s"
}
}
}
}`, n, n, nodeDC, k8sVersion, clientID, clientSecret, tenantID, subscID)
}`, n, n, nodeDC, k8sVersion, clientID, clientSecret, tenantID, subscID, billingTenant)
}

func TestAccMetakubeCluster_AWS_Basic(t *testing.T) {
Expand All @@ -633,14 +635,15 @@ func TestAccMetakubeCluster_AWS_Basic(t *testing.T) {
vpcID := os.Getenv(testEnvAWSVPCID)
nodeDC := os.Getenv(testEnvAWSNodeDC)
k8sVersion17 := os.Getenv(testEnvK8sVersion)
billingTenant := os.Getenv(testEnvOpenstackTenant)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckForAWS(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMetaKubeClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckMetaKubeClusterAWSBasic(clusterName, awsAccessKeyID, awsSecretAccessKey, vpcID, nodeDC, k8sVersion17),
Config: testAccCheckMetaKubeClusterAWSBasic(clusterName, awsAccessKeyID, awsSecretAccessKey, vpcID, nodeDC, billingTenant, k8sVersion17),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckMetaKubeClusterExists(&cluster),
resource.TestCheckResourceAttr(resourceName, "spec.0.cloud.0.aws.#", "1"),
Expand All @@ -650,7 +653,7 @@ func TestAccMetakubeCluster_AWS_Basic(t *testing.T) {
})
}

func testAccCheckMetaKubeClusterAWSBasic(n, keyID, keySecret, vpcID, nodeDC, k8sVersion string) string {
func testAccCheckMetaKubeClusterAWSBasic(n, keyID, keySecret, vpcID, nodeDC, billingTenant, k8sVersion string) string {
return fmt.Sprintf(`
resource "metakube_project" "acctest_project" {
name = "%s"
Expand All @@ -668,10 +671,11 @@ func testAccCheckMetaKubeClusterAWSBasic(n, keyID, keySecret, vpcID, nodeDC, k8s
access_key_id = "%s"
secret_access_key = "%s"
vpc_id = "%s"
openstack_billing_tenant = "%s"
}
}
}
}`, n, n, nodeDC, k8sVersion, keyID, keySecret, vpcID)
}`, n, n, nodeDC, k8sVersion, keyID, keySecret, vpcID, billingTenant)
}

func testAccCheckMetaKubeClusterDestroy(s *terraform.State) error {
Expand Down

0 comments on commit a6c445f

Please sign in to comment.