Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/plugin-framework-development' in…
Browse files Browse the repository at this point in the history
…to isaac/test-suites
  • Loading branch information
IsaacCalligeros95 committed Jul 29, 2024
2 parents fe1619e + 29311f0 commit 894e5c4
Show file tree
Hide file tree
Showing 30 changed files with 302 additions and 134 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ Documentation is auto-generated by the [tfplugindocs CLI](https://github.com/has
$ make docs
```

or
```shell
go generate main.go
```

## 🤝 Contributions

Contributions are welcome! :heart: Please read our [Contributing Guide](CONTRIBUTING.md) for information about how to get involved in this project.
9 changes: 0 additions & 9 deletions docs/data-sources/tenants.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,7 @@ Read-Only:
- `description` (String) The description of this tenant.
- `id` (String) The unique ID for this resource.
- `name` (String) The name of this resource.
- `project_environment` (Set of Object) (see [below for nested schema](#nestedatt--tenants--project_environment))
- `space_id` (String) The space ID associated with this resource.
- `tenant_tags` (List of String) A list of tenant tags associated with this resource.

<a id="nestedatt--tenants--project_environment"></a>
### Nested Schema for `tenants.project_environment`

Read-Only:

- `environments` (List of String)
- `project_id` (String)


14 changes: 1 addition & 13 deletions docs/resources/tenant.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "octopusdeploy_tenant Resource - terraform-provider-octopusdeploy"
subcategory: ""
description: |-
Expand All @@ -10,8 +9,6 @@ description: |-

This resource manages tenants in Octopus Deploy.



<!-- schema generated by tfplugindocs -->
## Schema

Expand All @@ -24,16 +21,7 @@ This resource manages tenants in Octopus Deploy.
- `cloned_from_tenant_id` (String) The ID of the tenant from which this tenant was cloned.
- `description` (String) The description of this tenant.
- `id` (String) The unique ID for this resource.
- `project_environment` (Block Set) (see [below for nested schema](#nestedblock--project_environment))
- `space_id` (String) The space ID associated with this resource.
- `tenant_tags` (List of String) A list of tenant tags associated with this resource.

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

Required:

- `environments` (List of String) A list of environment IDs associated with this tenant through a project.
- `project_id` (String) The project ID associated with this tenant.


~> **NOTE property `project_environment` deprecated:** The `project_environment` property has been replaced by the `octopusdeploy_tenant_project` resource to allow more advanced provisioning scenarioes.
32 changes: 32 additions & 0 deletions docs/resources/tenant_project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "octopusdeploy_tenant_project Resource - terraform-provider-octopusdeploy"
subcategory: ""
description: |-
This resource represents the connection between tenants and projects.
---

# octopusdeploy_tenant_project (Resource)

This resource represents the connection between tenants and projects.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `project_id` (String) The project ID associated with this tenant.
- `tenant_id` (String) The tenant ID associated with this tenant.

### Optional

- `environment_ids` (List of String) The environment ID associated with this tenant.
- `space_id` (String) The space ID associated with this resource.

### Read-Only

- `id` (String) The ID of this resource.


2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ github.com/Microsoft/hcsshim v0.12.4 h1:Ev7YUMHAHoWNm+aDSPzc5W9s6E2jyL1szpVDJeZ/
github.com/Microsoft/hcsshim v0.12.4/go.mod h1:Iyl1WVpZzr+UkzjekHZbV8o5Z9ZkxNGx6CtY2Qg/JVQ=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.43.0 h1:fYwGBqG88xy3qHp5j1ySCztdqfw2NLfg2yp0N3XcBYg=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.43.0/go.mod h1:GZmFu6LmN8Yg0tEoZx3ytk9FnaH+84cWm7u5TdWZC6E=
github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20240725030800-42853d105802 h1:w88wUy/7lsTfA04DWinhCVoqGFxSy5ZVKZetGUWYc5Y=
github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20240725030800-42853d105802/go.mod h1:Oq9KbiRNDBB5jFmrwnrgLX0urIqR/1ptY18TzkqXm7M=
github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20240729041805-46db6fb717b4 h1:QfbVf0bOIRMp/WHAWsuVDB7KHoWnRsGbvDuOf2ua7k4=
github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20240729041805-46db6fb717b4/go.mod h1:Oq9KbiRNDBB5jFmrwnrgLX0urIqR/1ptY18TzkqXm7M=
github.com/ProtonMail/go-crypto v1.1.0-alpha.2 h1:bkyFVUP+ROOARdgCiJzNQo2V2kiB97LyUpzH9P6Hrlg=
Expand Down
5 changes: 3 additions & 2 deletions octopusdeploy/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func Provider() *schema.Provider {
"octopusdeploy_tag_set": resourceTagSet(),
"octopusdeploy_team": resourceTeam(),
"octopusdeploy_tenant": resourceTenant(),
"octopusdeploy_tenant_project": resourceTenantProject(),
"octopusdeploy_tenant_common_variable": resourceTenantCommonVariable(),
"octopusdeploy_tenant_project_variable": resourceTenantProjectVariable(),
"octopusdeploy_tentacle_certificate": resourceTentacleCertificate(),
Expand All @@ -90,13 +91,13 @@ func Provider() *schema.Provider {
"address": {
DefaultFunc: schema.EnvDefaultFunc("OCTOPUS_URL", nil),
Description: "The endpoint of the Octopus REST API",
Required: true,
Optional: true,
Type: schema.TypeString,
},
"api_key": {
DefaultFunc: schema.EnvDefaultFunc("OCTOPUS_APIKEY", nil),
Description: "The API key to use with the Octopus REST API",
Required: true,
Optional: true,
Type: schema.TypeString,
},
"space_id": {
Expand Down
158 changes: 158 additions & 0 deletions octopusdeploy/resource_tenant project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package octopusdeploy

import (
"context"
"log"
"net/http"
"strings"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/tenants"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceTenantProject() *schema.Resource {
return &schema.Resource{
CreateContext: resourceTenantProjectCreate,
DeleteContext: resourceTenantProjectDelete,
Description: "This resource represents the connection between tenants and projects.",
Importer: getImporter(),
ReadContext: resourceTenantProjectRead,
UpdateContext: resourceTenantProjectUpdate,
Schema: getTenantProjectSchema(),
}
}

func resourceTenantProjectUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()

client := m.(*client.Client)
k := extractRelationship(d, client)

log.Printf("[INFO] updating tenant (%#v) connection to project (%#v)", k.tenantID, k.projectID)

tenant, err := tenants.GetByID(client, k.spaceID, k.tenantID)
if err != nil {
return diag.FromErr(err)
}

tenant.ProjectEnvironments[k.projectID] = k.environmentIDs

_, err = tenants.Update(client, tenant)
if err != nil {
return diag.FromErr(err)
}

log.Printf("[INFO] updated tenant (%s) connection to project (%#v)", k.tenantID, k.projectID)
return nil
}

func resourceTenantProjectCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()

client := m.(*client.Client)
k := extractRelationship(d, client)

log.Printf("[INFO] connecting tenant (%#v) to project (%#v)", k.tenantID, k.projectID)

tenant, err := tenants.GetByID(client, k.spaceID, k.tenantID)
if err != nil {
return diag.FromErr(err)
}

tenant.ProjectEnvironments[k.projectID] = k.environmentIDs

_, err = tenants.Update(client, tenant)
if err != nil {
return diag.FromErr(err)
}

id := k.spaceID + ":" + k.tenantID + ":" + k.projectID
d.SetId(id)

log.Printf("[INFO] tenant (%s) connected to project (%#v)", k.tenantID, k.projectID)
return nil
}

func resourceTenantProjectDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()

client := m.(*client.Client)
k := extractRelationship(d, client)

log.Printf("[INFO] removing tenant (%#v) from project (%#v)", k.tenantID, k.projectID)

tenant, err := tenants.GetByID(client, k.spaceID, k.tenantID)
if err != nil {
apiError := err.(*core.APIError)
if apiError.StatusCode == http.StatusNotFound {
log.Printf("[INFO] tenant (%#v) no longer exists", k.tenantID)
d.SetId("")
return nil
}

return diag.FromErr(err)
}

delete(tenant.ProjectEnvironments, k.projectID)

_, err = tenants.Update(client, tenant)
if err != nil {
return diag.FromErr(err)
}

log.Printf("[INFO] tenant (%#v) disconnected from project (%#v)", k.tenantID, k.projectID)
d.SetId("")
return nil
}

func resourceTenantProjectRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*client.Client)

bits := strings.Split(d.Id(), ":")
spaceID := bits[0]
tenantID := bits[1]
projectID := bits[2]

tenant, err := tenants.GetByID(client, spaceID, tenantID)
if err != nil {
apiError := err.(*core.APIError)
if apiError.StatusCode != http.StatusNotFound {
return diag.FromErr(err)
}
}

d.Set("environment_ids", tenant.ProjectEnvironments[projectID])

return nil
}

func extractRelationship(d *schema.ResourceData, client *client.Client) person {
tenantID := d.Get("tenant_id").(string)
projectID := d.Get("project_id").(string)

environmentIDs := []string{}
if attr, ok := d.GetOk("environment_ids"); ok {
environmentIDs = getSliceFromTerraformTypeList(attr)
}

spaceID := client.GetSpaceID()
if v, ok := d.GetOk("space_id"); ok {
spaceID = v.(string)
}

n := person{tenantID: tenantID, projectID: projectID, environmentIDs: environmentIDs, spaceID: spaceID}
return n
}

type person struct {
tenantID string
projectID string
environmentIDs []string
spaceID string
}
9 changes: 9 additions & 0 deletions octopusdeploy/resource_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func resourceTenant() *schema.Resource {
}

func resourceTenantCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()

tenant := expandTenant(d)

log.Printf("[INFO] creating tenant: %#v", tenant)
Expand All @@ -45,6 +48,9 @@ func resourceTenantCreate(ctx context.Context, d *schema.ResourceData, m interfa
}

func resourceTenantDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()

log.Printf("[INFO] deleting tenant (%s)", d.Id())

client := m.(*client.Client)
Expand Down Expand Up @@ -75,6 +81,9 @@ func resourceTenantRead(ctx context.Context, d *schema.ResourceData, m interface
}

func resourceTenantUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()

log.Printf("[INFO] updating tenant (%s)", d.Id())

tenant := expandTenant(d)
Expand Down
40 changes: 0 additions & 40 deletions octopusdeploy/schema_project_environment.go

This file was deleted.

Loading

0 comments on commit 894e5c4

Please sign in to comment.