Skip to content

Commit

Permalink
Add Azure to Cloud Connection (#27)
Browse files Browse the repository at this point in the history
* Add Azure to Cloud Connection

* Add other method of local development

* Update cloud_connection_cloud_provider.go

* Add Azure description
  • Loading branch information
mitch-hamm authored Feb 27, 2024
1 parent 227bca7 commit b15d325
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ gen

temp/**

.vscode/
.vscode/
sa_key.json
33 changes: 33 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,36 @@ If you would like to contribute code to this project, fork the repository and se
| MacOS(amd64) | ~/.terraform.d/plugins/registry.terraform.io/streamnative/streamnative/0.1.0/darwin_amd64/ |

- Run `make build-dev`, it will build the binary and copy it to the plugin directory automatically.

## OR

## Using .terraformrc

- Make sure GOBIN is set (if not set it to `/Users/<Username>/go/bin`)
- Create a file in `~` named `.terraformrc`
- Add the following into the file
```
provider_installation {
dev_overrides {
"terraform.local/local/streamnative" = "/Users/<Username>/go/bin" #Or your GOBIN if it's defined as a different path
}
# For all other providers, install them directly from their origin provider
# registries as normal. If you omit this, Terraform will _only_ use
# the dev_overrides block, and so no other providers will be available.
direct {}
}
```
- Run `go install .` in the provider root
- Use the provider in terraform like so
```
terraform {
required_providers {
streamnative = {
source = "terraform.local/local/streamnative"
}
}
}
```
- Run a terraform plan and terraform should use the newly built copy
21 changes: 21 additions & 0 deletions cloud/cloud_connection_cloud_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,24 @@ func flattenCloudConnectionGCP(in *cloudv1alpha1.GCPCloudConnection) []interface

return []interface{}{att}
}

func flattenCloudConnectionAzure(in *cloudv1alpha1.AzureConnection) []interface{} {
att := make(map[string]interface{})
if in.SubscriptionId != "" {
att["subscription_id"] = in.SubscriptionId
}

if in.TenantId != "" {
att["tenant_id"] = in.TenantId
}

if in.ClientId != "" {
att["client_id"] = in.ClientId
}

if in.SupportClientId != "" {
att["support_client_id"] = in.SupportClientId
}

return []interface{}{att}
}
32 changes: 32 additions & 0 deletions cloud/data_source_cloud_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ func dataSourceCloudConnection() *schema.Resource {
},
},
},
"azure": {
Type: schema.TypeList,
Computed: true,
Description: descriptions["azure"],
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subscription_id": {
Type: schema.TypeString,
Optional: true,
},
"tenant_id": {
Type: schema.TypeString,
Optional: true,
},
"client_id": {
Type: schema.TypeString,
Optional: true,
},
"support_client_id": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -116,6 +141,13 @@ func dataSourceCloudConnectionRead(ctx context.Context, d *schema.ResourceData,
}
}

if cloudConnection.Spec.Azure != nil {
err = d.Set("azure", flattenCloudConnectionAzure(cloudConnection.Spec.Azure))
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_READ_CLOUD_CONNECTION_CONFIG: %w", err))
}
}

d.SetId(fmt.Sprintf("%s/%s", cloudConnection.Namespace, cloudConnection.Name))

return nil
Expand Down
1 change: 1 addition & 0 deletions cloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func init() {
"type": "Type of cloud connection, one of aws or gcp",
"aws": "AWS configuration for the connection",
"gcp": "GCP configuration for the connection",
"azure": "Azure configuration for the connection",
"cloud_connection_name": "Name of the cloud connection",
"cloud_environment_name": "Name of the cloud environment",
}
Expand Down
58 changes: 56 additions & 2 deletions cloud/resource_cloud_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,31 @@ func resourceCloudConnection() *schema.Resource {
},
},
},
"azure": {
Type: schema.TypeList,
Optional: true,
Description: descriptions["azure"],
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subscription_id": {
Type: schema.TypeString,
Optional: true,
},
"tenant_id": {
Type: schema.TypeString,
Optional: true,
},
"client_id": {
Type: schema.TypeString,
Optional: true,
},
"support_client_id": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
}
}
Expand All @@ -116,6 +141,7 @@ func resourceCloudConnectionCreate(ctx context.Context, d *schema.ResourceData,
connectionType := d.Get("type").(string)
aws := d.Get("aws").([]interface{})
gcp := d.Get("gcp").([]interface{})
azure := d.Get("azure").([]interface{})
clientSet, err := getClientSet(getFactoryFromMeta(meta))
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_INIT_CLIENT_ON_CLOUD_CONNECTION: %w", err))
Expand All @@ -135,6 +161,7 @@ func resourceCloudConnectionCreate(ctx context.Context, d *schema.ResourceData,
ConnectionType: cloudv1alpha1.ConnectionType(connectionType),
AWS: nil,
GCP: nil,
Azure: nil,
},
}

Expand All @@ -160,8 +187,27 @@ func resourceCloudConnectionCreate(ctx context.Context, d *schema.ResourceData,
}
}

if cloudConnection.Spec.AWS == nil && cloudConnection.Spec.GCP == nil {
return diag.FromErr(fmt.Errorf("ERROR_CREATE_CLOUD_CONNECTION: " + "One of aws.accountId or gcp.project_id must be set"))
if len(azure) > 0 {
cloudConnection.Spec.Azure = &cloudv1alpha1.AzureConnection{}
for _, azureItem := range azure {
azureItemMap := azureItem.(map[string]interface{})
if azureItemMap["subscription_id"] != nil {
cloudConnection.Spec.Azure.SubscriptionId = azureItemMap["subscription_id"].(string)
}
if azureItemMap["tenant_id"] != nil {
cloudConnection.Spec.Azure.TenantId = azureItemMap["tenant_id"].(string)
}
if azureItemMap["client_id"] != nil {
cloudConnection.Spec.Azure.ClientId = azureItemMap["client_id"].(string)
}
if azureItemMap["support_client_id"] != nil {
cloudConnection.Spec.Azure.SupportClientId = azureItemMap["support_client_id"].(string)
}
}
}

if cloudConnection.Spec.AWS == nil && cloudConnection.Spec.GCP == nil && cloudConnection.Spec.Azure == nil {
return diag.FromErr(fmt.Errorf("ERROR_CREATE_CLOUD_CONNECTION: " + "One of aws.account_id, gcp.project_id or azure block must be set"))
}

cc, err := clientSet.CloudV1alpha1().CloudConnections(namespace).Create(ctx, cloudConnection, metav1.CreateOptions{
Expand Down Expand Up @@ -221,6 +267,14 @@ func resourceCloudConnectionRead(ctx context.Context, d *schema.ResourceData, me
return diag.FromErr(fmt.Errorf("ERROR_READ_CLOUD_CONNECTION_GCP: %w", err))
}
}

if cloudConnection.Spec.Azure != nil {
err = d.Set("azure", flattenCloudConnectionAzure(cloudConnection.Spec.Azure))
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_READ_CLOUD_CONNECTION_AZURE: %w", err))
}
}

d.SetId(fmt.Sprintf("%s/%s", cloudConnection.Namespace, cloudConnection.Name))
return nil
}
Expand Down
12 changes: 12 additions & 0 deletions docs/data-sources/cloud_connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ description: |-
### Read-Only

- `aws` (List of Object) AWS configuration for the connection (see [below for nested schema](#nestedatt--aws))
- `azure` (List of Object) Azure configuration for the connection (see [below for nested schema](#nestedatt--azure))
- `gcp` (List of Object) GCP configuration for the connection (see [below for nested schema](#nestedatt--gcp))
- `id` (String) The ID of this resource.
- `type` (String) Type of cloud connection, one of aws or gcp
Expand All @@ -35,6 +36,17 @@ Read-Only:
- `account_id` (String)


<a id="nestedatt--azure"></a>
### Nested Schema for `azure`

Read-Only:

- `client_id` (String)
- `subscription_id` (String)
- `support_client_id` (String)
- `tenant_id` (String)


<a id="nestedatt--gcp"></a>
### Nested Schema for `gcp`

Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ description: |-
<!-- schema generated by tfplugindocs -->
## Schema

### Optional
### Required

- `key_file_path` (String) The path of the private key file
12 changes: 12 additions & 0 deletions docs/resources/cloud_connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ description: |-
### Optional

- `aws` (Block List) AWS configuration for the connection (see [below for nested schema](#nestedblock--aws))
- `azure` (Block List) Azure configuration for the connection (see [below for nested schema](#nestedblock--azure))
- `gcp` (Block List) GCP configuration for the connection (see [below for nested schema](#nestedblock--gcp))

### Read-Only
Expand All @@ -38,6 +39,17 @@ Optional:
- `account_id` (String)


<a id="nestedblock--azure"></a>
### Nested Schema for `azure`

Optional:

- `client_id` (String)
- `subscription_id` (String)
- `support_client_id` (String)
- `tenant_id` (String)


<a id="nestedblock--gcp"></a>
### Nested Schema for `gcp`

Expand Down

0 comments on commit b15d325

Please sign in to comment.