Skip to content

Commit

Permalink
Merge pull request #57 from syseleven/use_project_id
Browse files Browse the repository at this point in the history
use openstack project id and delete openstack project name
  • Loading branch information
furkhat authored May 25, 2022
2 parents 3398e5b + f008ca1 commit 6edeab2
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 149 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ DOMAIN=syseleven.de
NAMESPACE=syseleven
PKG_NAME=metakube
BINARY=terraform-provider-${PKG_NAME}
VERSION=1.2.0
VERSION=3.0.0
PLATFORM?=darwin_arm64
SWEEP_DIR?=./metakube
SWEEP?=all
Expand Down
6 changes: 3 additions & 3 deletions docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ When set, start time and length must be configured.
* `subnet_cidr` - (Optional) Change this to configure a different internal IP range for Nodes. Default: `192.168.1.0/24`.
When using password based auth
* `server_group_id` - (Optional) Server group id to use for all machines within a cluster. You can use openstack server groups to group or seperate servers using soft/hard affinity/anti-affinity rules. When not set explicitly, the default soft anti-affinity server group will be created and used.
* `tenant` - (Optional) The project to use for billing. You can set it using environment variable `OS_PROJECT_NAME`. Must be omit if application credentials are used.
* `project_id` - (Optional) The id of project to use for billing. You can set it using environment variable `OS_PROJECT_NAME`. Must be omit if application credentials are used.
* `username` - (Optional) The account's username. You can set it using environment variable `OS_USERNAME`. Must be omit if application credentials are used.
* `password` - (Optional) The account's password. You can set it using environment variable `OS_PASSWORD`. Must be omit if application credentials are used.
When using application credentials
* `application_credentials_id` - (Opitonal) Application credentials ID to use. Must be omit if username/password/tenant are used.
* `application_credentials_secret` - (Opitonal) Application credentials Secret to use. Must be omit if username/password/tenant are used.
* `application_credentials_id` - (Opitonal) Application credentials ID to use. Must be omit if username/password/project_id are used.
* `application_credentials_secret` - (Opitonal) Application credentials Secret to use. Must be omit if username/password/project_id are used.

### `aws`

Expand Down
3 changes: 1 addition & 2 deletions examples/openstack/advanced/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ To run this example:
1. set up your OpenStack provider credentials. You can do that by downloading and sourcing "OpenStack RC File v3"
for your account at https://cloud.syseleven.de or configure it manually by uncommenting [corresponding section in
terraform file](./main.tf#{L12:L23}) configuration.
2. If you sourced "OpenStack RC File v3" please also set `OS_PROJECT_NAME` that rc file does not have. You can get project name by running openstack cli `openstack project show $OS_PROJECT_ID -c name`.
3. Configure MetaKube host and token as described in the [documentation](https://registry.terraform.io/providers/syseleven/metakube/latest/docs)
2. Configure MetaKube host and token as described in the [documentation](https://registry.terraform.io/providers/syseleven/metakube/latest/docs)

Running the example
```
Expand Down
10 changes: 5 additions & 5 deletions examples/openstack/advanced/main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
terraform {
required_providers {
metakube = {
source = "syseleven/metakube"
}
metakube = {
source = "syseleven/metakube"
}
openstack = {
source = "terraform-provider-openstack/openstack"
}
Expand Down Expand Up @@ -92,7 +92,7 @@ resource "openstack_networking_subnet_v2" "subnet_1" {
}

resource "openstack_compute_servergroup_v2" "anti-affinity" {
name = "test-anti-affinity"
name = "test-anti-affinity"
policies = ["anti-affinity"]
}

Expand Down Expand Up @@ -157,7 +157,7 @@ resource "metakube_cluster" "cluster" {
version = data.metakube_k8s_version.cluster.version
cloud {
openstack {
tenant = var.tenant
project_id = var.openstack_project_id
username = var.username
password = var.password
floating_ip_pool = data.openstack_networking_network_v2.external.name
Expand Down
4 changes: 2 additions & 2 deletions examples/openstack/advanced/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ variable "password" {
default = null
}

variable "tenant" {
description = "OpenStack tenant"
variable "openstack_project_id" {
description = "OpenStack project id"
type = string
default = null
}
Expand Down
3 changes: 1 addition & 2 deletions examples/openstack/basic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ To run this example:
1. set up your OpenStack provider credentials. You can do that by downloading and sourcing "OpenStack RC File v3"
for your account at https://cloud.syseleven.de or configure it manually by uncommenting [corresponding section in
the file](./main.tf#{L12:L23}).
2. If you sourced "OpenStack RC File v3" please also set `OS_PROJECT_NAME` that rc file does not have. You can get project name by running openstack cli `openstack project show $OS_PROJECT_ID -c name`.
3. configure MetaKube host and token as described in the [documentation](https://registry.terraform.io/providers/syseleven/metakube/latest/docs)
2. configure MetaKube host and token as described in the [documentation](https://registry.terraform.io/providers/syseleven/metakube/latest/docs)

Running the example
```
Expand Down
2 changes: 1 addition & 1 deletion examples/openstack/basic/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ resource "metakube_cluster" "cluster" {
openstack {
floating_ip_pool = var.floating_ip_pool
password = var.password
tenant = var.tenant
project_id = var.openstack_project_id
username = var.username
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/openstack/basic/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ variable "password" {
default = null
}

variable "tenant" {
description = "OpenStack tenant"
variable "openstack_project_id" {
description = "OpenStack project id"
type = string
default = null
}
Expand Down
6 changes: 3 additions & 3 deletions metakube/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
testEnvOpenstackUsername = "METAKUBE_OPENSTACK_USERNAME"
testEnvOpenstackAuthURL = "METAKUBE_OPENSTACK_AUTH_URL"
testEnvOpenstackPassword = "METAKUBE_OPENSTACK_PASSWORD"
testEnvOpenstackTenant = "METAKUBE_OPENSTACK_TENANT"
testEnvOpenstackProjectID = "METAKUBE_OPENSTACK_PROJECT_ID"
testEnvOpenstackImage = "METAKUBE_OPENSTACK_IMAGE"
testEnvOpenstackImage2 = "METAKUBE_OPENSTACK_IMAGE2"
testEnvOpenstackImageFlatcar = "METAKUBE_OPENSTACK_IMAGE_FLATCAR"
Expand Down Expand Up @@ -79,7 +79,7 @@ func testAccPreCheckForOpenstack(t *testing.T) {
testAccPreCheck(t)
checkEnv(t, testEnvOpenstackUsername)
checkEnv(t, testEnvOpenstackPassword)
checkEnv(t, testEnvOpenstackTenant)
checkEnv(t, testEnvOpenstackProjectID)
checkEnv(t, testEnvOpenstackNodeDC)
checkEnv(t, testEnvOpenstackImage)
checkEnv(t, testEnvOpenstackImage2)
Expand All @@ -96,7 +96,7 @@ func testAccPreCheckForAzure(t *testing.T) {
checkEnv(t, testEnvAzureTenantID)
checkEnv(t, testEnvAzureNodeDC)
checkEnv(t, testEnvAzureNodeSize)
checkEnv(t, testEnvOpenstackTenant)
checkEnv(t, testEnvOpenstackProjectID)
}

func testAccPreCheckForAWS(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions metakube/resource_metakube_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/syseleven/go-metakube/client/datacenter"
"github.com/syseleven/go-metakube/client/project"
"github.com/syseleven/go-metakube/models"
Expand Down Expand Up @@ -532,7 +531,7 @@ type clusterPreserveValues struct {
type clusterOpenstackPreservedValues struct {
openstackUsername interface{}
openstackPassword interface{}
openstackTenant interface{}
openstackProjectID interface{}
openstackServerGroupID interface{}
openstackApplicationCredentialsID interface{}
openstackApplicationCredentialsSecret interface{}
Expand All @@ -547,7 +546,7 @@ func readClusterPreserveValues(d *schema.ResourceData) clusterPreserveValues {
openstack = &clusterOpenstackPreservedValues{
openstackUsername: d.Get(key("openstack.0.username")),
openstackPassword: d.Get(key("openstack.0.password")),
openstackTenant: d.Get(key("openstack.0.tenant")),
openstackProjectID: d.Get(key("openstack.0.project_id")),
openstackServerGroupID: d.Get(key("openstack.0.server_group_id")),
openstackApplicationCredentialsID: d.Get(key("openstack.0.application_credentials_id")),
openstackApplicationCredentialsSecret: d.Get(key("openstack.0.application_credentials_secret")),
Expand Down
59 changes: 55 additions & 4 deletions metakube/resource_metakube_cluster_schema.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package metakube

import (
"context"
"fmt"
"regexp"
"time"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/syseleven/go-metakube/client/openstack"
)

func metakubeResourceClusterSpecFields() map[string]*schema.Schema {
Expand Down Expand Up @@ -76,6 +81,52 @@ func metakubeResourceClusterSpecFields() map[string]*schema.Schema {
MaxItems: 1,
Description: "OpenStack cluster specification",
Elem: &schema.Resource{
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Version: 0,
Type: cty.Object(map[string]cty.Type{
"username": cty.String,
"tenant": cty.String,
"password": cty.String,
"application_credentials_id": cty.String,
}),
Upgrade: func(ctx context.Context, rawState map[string]interface{}, m interface{}) (map[string]interface{}, error) {
if v, ok := rawState["application_credential_id"].(string); ok && v != "" {
return rawState, nil
}

meta := m.(*metakubeProviderMeta)
if u, ok := rawState["username"].(string); !ok {
return nil, fmt.Errorf("could not read 'username' %v from state %v", rawState["username"], rawState)
} else if p, ok := rawState["password"].(string); !ok {
return nil, fmt.Errorf("could not read 'password' %v from state %v", rawState["password"], rawState)
} else if name, ok := rawState["tenant"].(string); !ok {
return nil, fmt.Errorf("could not read 'tenant' %v from state %v", rawState["tenant"], rawState)
} else {
d := "Default"
params := openstack.NewListOpenstackTenantsParams().WithContext(ctx).WithDomain(&d).WithUsername(&u).WithPassword(&p)
result, err := meta.client.Openstack.ListOpenstackTenants(params, meta.auth)
if err != nil {
return nil, fmt.Errorf("could not get tenant '%s' id: %v", name, err)
}
var id string
for _, v := range result.Payload {
if v.Name == name {
id = v.ID
break
}
}
if id == "" {
return nil, fmt.Errorf("cound not find tenant '%s'", name)
}
delete(rawState, "tenant")
rawState["project_id"] = id
return rawState, nil
}
},
},
},
Schema: metakubeResourceClusterOpenstackCloudSpecFields(),
},
ConflictsWith: []string{"spec.0.cloud.0.aws", "spec.0.cloud.0.azure"},
Expand Down Expand Up @@ -286,12 +337,12 @@ func metakubeResourceCluserAWSCloudSpecFields() map[string]*schema.Schema {

func metakubeResourceClusterOpenstackCloudSpecFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"tenant": {
"project_id": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"spec.0.cloud.0.openstack.0.application_credentials_id", "spec.0.cloud.0.openstack.0.application_credentials_secret"},
DefaultFunc: schema.EnvDefaultFunc("OS_PROJECT_NAME", nil),
Description: "The opestack project to use for billing",
DefaultFunc: schema.EnvDefaultFunc("OS_PROJECT_ID", nil),
Description: "The id of opestack project to use for billing",
},
"username": {
Type: schema.TypeString,
Expand All @@ -311,7 +362,7 @@ func metakubeResourceClusterOpenstackCloudSpecFields() map[string]*schema.Schema
},
"application_credentials_id": {
Type: schema.TypeString,
ConflictsWith: []string{"spec.0.cloud.0.openstack.0.username", "spec.0.cloud.0.openstack.0.password", "spec.0.cloud.0.openstack.0.tenant"},
ConflictsWith: []string{"spec.0.cloud.0.openstack.0.username", "spec.0.cloud.0.openstack.0.password", "spec.0.cloud.0.openstack.0.project_id"},
Optional: true,
Description: "Openstack application credentials ID",
},
Expand Down
16 changes: 4 additions & 12 deletions metakube/resource_metakube_cluster_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ func metakubeResourceClusterFlattenSpec(values clusterPreserveValues, in *models
return []interface{}{att}
}

func metakubeResourceClusterFlattenSys11AuthClusterRoleBindings(in []models.ClusterRoleBinding) []interface{} {
return nil
}

func metakubeResourceClusterFlattenSys11AuthRoleBindings(in []models.ClusterRoleBinding) []interface{} {
return nil
}

func flattenUpdateWindow(in *models.UpdateWindow) []interface{} {
m := make(map[string]interface{})
m["start"] = in.Start
Expand Down Expand Up @@ -209,8 +201,8 @@ func flattenOpenstackSpec(values *clusterOpenstackPreservedValues, in *models.Op
if _, ok := att["server_group_id"]; !ok && values.openstackServerGroupID != nil {
att["server_group_id"] = values.openstackServerGroupID
}
if values.openstackTenant != nil {
att["tenant"] = values.openstackTenant
if values.openstackProjectID != nil {
att["project_id"] = values.openstackProjectID
}
if values.openstackUsername != nil {
att["username"] = values.openstackUsername
Expand Down Expand Up @@ -554,9 +546,9 @@ func expandOpenstackCloudSpec(p []interface{}) *models.OpenstackCloudSpec {
}
in := p[0].(map[string]interface{})

if v, ok := in["tenant"]; ok {
if v, ok := in["project_id"]; ok {
if vv, ok := v.(string); ok && vv != "" {
obj.Tenant = vv
obj.TenantID = vv
}
}

Expand Down
20 changes: 10 additions & 10 deletions metakube/resource_metakube_cluster_structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,21 +221,21 @@ func TestFlattenOpenstackCloudSpec(t *testing.T) {
RouterID: "RouterID",
SecurityGroups: "SecurityGroups",
SubnetID: "SubnetID",
Tenant: "",
TenantID: "TenantID",
Tenant: "Tenant",
TenantID: "",
Username: "",
ServerGroupID: "ServerGroupID",
},
clusterOpenstackPreservedValues{
openstackUsername: "Username",
openstackPassword: "Password",
openstackTenant: "Tenant",
openstackUsername: "Username",
openstackPassword: "Password",
openstackProjectID: "TenantID",
},
[]interface{}{
map[string]interface{}{
"username": "Username",
"password": "Password",
"tenant": "Tenant",
"project_id": "TenantID",
"floating_ip_pool": "FloatingIPPool",
"network": "Network",
"security_group": "SecurityGroups",
Expand All @@ -253,21 +253,21 @@ func TestFlattenOpenstackCloudSpec(t *testing.T) {
RouterID: "RouterID",
SecurityGroups: "SecurityGroups",
SubnetID: "SubnetID",
Tenant: "",
TenantID: "TenantID",
Tenant: "Tenant",
TenantID: "",
Username: "",
},
clusterOpenstackPreservedValues{
openstackUsername: "Username",
openstackPassword: "Password",
openstackTenant: "Tenant",
openstackProjectID: "TenantID",
openstackServerGroupID: "ServerGroupID",
},
[]interface{}{
map[string]interface{}{
"username": "Username",
"password": "Password",
"tenant": "Tenant",
"project_id": "TenantID",
"floating_ip_pool": "FloatingIPPool",
"network": "Network",
"security_group": "SecurityGroups",
Expand Down
Loading

0 comments on commit 6edeab2

Please sign in to comment.