Skip to content

Commit

Permalink
Added support for teams
Browse files Browse the repository at this point in the history
Implemented #125
  • Loading branch information
jbristowe committed Jan 29, 2021
1 parent d5e5349 commit 5d96f7f
Show file tree
Hide file tree
Showing 8 changed files with 517 additions and 0 deletions.
78 changes: 78 additions & 0 deletions docs/data-sources/teams.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
page_title: "octopusdeploy_teams Data Source - terraform-provider-octopusdeploy"
subcategory: ""
description: |-
Provides information about existing users.
---

# Data Source `octopusdeploy_teams`

Provides information about existing users.



## Schema

### Optional

- **ids** (List of String, Optional) A filter to search by a list of IDs.
- **include_system** (Boolean, Optional) A filter to include system teams.
- **partial_name** (String, Optional) A filter to search by the partial match of a name.
- **skip** (Number, Optional) A filter to specify the number of items to skip in the response.
- **take** (Number, Optional) A filter to specify the number of items to take (or return) in the response.

### Read-only

- **id** (String, Read-only) A auto-generated identifier that includes the timestamp when this data source was last modified.
- **spaces** (Block List) A list of spaces that match the filter(s). (see [below for nested schema](#nestedblock--spaces))
- **teams** (Block List) A list of teams that match the filter(s). (see [below for nested schema](#nestedblock--teams))

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

Read-only:

- **can_be_deleted** (Boolean, Read-only)
- **can_be_renamed** (Boolean, Read-only)
- **can_change_members** (Boolean, Read-only)
- **can_change_roles** (Boolean, Read-only)
- **description** (String, Read-only)
- **external_security_groups** (List of Object, Read-only) (see [below for nested schema](#nestedatt--spaces--external_security_groups))
- **id** (String, Read-only) The unique ID for this resource.
- **name** (String, Read-only)
- **space_id** (String, Read-only)
- **users** (List of String, Read-only) A list of user IDs designated to be members of this team.

<a id="nestedatt--spaces--external_security_groups"></a>
### Nested Schema for `spaces.external_security_groups`

- **display_id_and_name** (Boolean)
- **display_name** (String)
- **id** (String)



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

Read-only:

- **can_be_deleted** (Boolean, Read-only)
- **can_be_renamed** (Boolean, Read-only)
- **can_change_members** (Boolean, Read-only)
- **can_change_roles** (Boolean, Read-only)
- **description** (String, Read-only)
- **external_security_groups** (List of Object, Read-only) (see [below for nested schema](#nestedatt--teams--external_security_groups))
- **id** (String, Read-only) The unique ID for this resource.
- **name** (String, Read-only)
- **space_id** (String, Read-only)
- **users** (List of String, Read-only) A list of user IDs designated to be members of this team.

<a id="nestedatt--teams--external_security_groups"></a>
### Nested Schema for `teams.external_security_groups`

- **display_id_and_name** (Boolean)
- **display_name** (String)
- **id** (String)


44 changes: 44 additions & 0 deletions docs/resources/team.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
page_title: "octopusdeploy_team Resource - terraform-provider-octopusdeploy"
subcategory: ""
description: |-
This resource manages teams in Octopus Deploy.
---

# Resource `octopusdeploy_team`

This resource manages teams in Octopus Deploy.



## Schema

### Required

- **name** (String, Required)

### Optional

- **can_be_deleted** (Boolean, Optional)
- **can_be_renamed** (Boolean, Optional)
- **can_change_members** (Boolean, Optional)
- **can_change_roles** (Boolean, Optional)
- **description** (String, Optional)
- **external_security_groups** (Block List) (see [below for nested schema](#nestedblock--external_security_groups))
- **id** (String, Optional) The unique ID for this resource.
- **users** (List of String, Optional) A list of user IDs designated to be members of this team.

### Read-only

- **space_id** (String, Read-only)

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

Optional:

- **display_id_and_name** (Boolean, Optional)
- **display_name** (String, Optional)
- **id** (String, Optional) The unique ID for this resource.


44 changes: 44 additions & 0 deletions octopusdeploy/data_source_teams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package octopusdeploy

import (
"context"
"time"

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

func dataSourceTeams() *schema.Resource {
return &schema.Resource{
Description: "Provides information about existing users.",
ReadContext: dataSourceTeamsRead,
Schema: getTeamDataSchema(),
}
}

func dataSourceTeamsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
query := octopusdeploy.TeamsQuery{
IDs: expandArray(d.Get("ids").([]interface{})),
IncludeSystem: d.Get("include_system").(bool),
PartialName: d.Get("partial_name").(string),
Skip: d.Get("skip").(int),
Take: d.Get("take").(int),
}

client := meta.(*octopusdeploy.Client)
users, err := client.Teams.Get(query)
if err != nil {
return diag.FromErr(err)
}

flattenedTeams := []interface{}{}
for _, user := range users.Items {
flattenedTeams = append(flattenedTeams, flattenTeam(user))
}

d.Set("teams", flattenedTeams)
d.SetId("Teams " + time.Now().UTC().String())

return nil
}
2 changes: 2 additions & 0 deletions octopusdeploy/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func Provider() *schema.Provider {
"octopusdeploy_spaces": dataSourceSpaces(),
"octopusdeploy_ssh_connection_deployment_targets": dataSourceSSHConnectionDeploymentTargets(),
"octopusdeploy_tag_sets": dataSourceTagSets(),
"octopusdeploy_teams": dataSourceTeams(),
"octopusdeploy_tenants": dataSourceTenants(),
"octopusdeploy_users": dataSourceUsers(),
"octopusdeploy_user_roles": dataSourceUserRoles(),
Expand Down Expand Up @@ -70,6 +71,7 @@ func Provider() *schema.Provider {
"octopusdeploy_ssh_connection_deployment_target": resourceSSHConnectionDeploymentTarget(),
"octopusdeploy_ssh_key_account": resourceSSHKeyAccount(),
"octopusdeploy_tag_set": resourceTagSet(),
"octopusdeploy_team": resourceTeam(),
"octopusdeploy_tenant": resourceTenant(),
"octopusdeploy_token_account": resourceTokenAccount(),
"octopusdeploy_user": resourceUser(),
Expand Down
98 changes: 98 additions & 0 deletions octopusdeploy/resource_team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package octopusdeploy

import (
"context"
"log"

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

func resourceTeam() *schema.Resource {
return &schema.Resource{
CreateContext: resourceTeamCreate,
DeleteContext: resourceTeamDelete,
Description: "This resource manages teams in Octopus Deploy.",
Importer: getImporter(),
ReadContext: resourceTeamRead,
Schema: getTeamSchema(),
UpdateContext: resourceTeamUpdate,
}
}

func resourceTeamCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
team := expandTeam(d)

log.Printf("[INFO] creating team: %#v", team)

client := m.(*octopusdeploy.Client)
createdTeam, err := client.Teams.Add(team)
if err != nil {
return diag.FromErr(err)
}

if err := setTeam(ctx, d, createdTeam); err != nil {
return diag.FromErr(err)
}

d.SetId(createdTeam.GetID())

log.Printf("[INFO] team created (%s)", d.Id())
return nil
}

func resourceTeamDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] deleting team (%s)", d.Id())

client := m.(*octopusdeploy.Client)
if err := client.Teams.DeleteByID(d.Id()); err != nil {
return diag.FromErr(err)
}

d.SetId("")

log.Printf("[INFO] team deleted")
return nil
}

func resourceTeamRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] reading team (%s)", d.Id())

client := m.(*octopusdeploy.Client)
team, err := client.Teams.GetByID(d.Id())
if err != nil {
apiError := err.(*octopusdeploy.APIError)
if apiError.StatusCode == 404 {
log.Printf("[INFO] team (%s) not found; deleting from state", d.Id())
d.SetId("")
return nil
}
return diag.FromErr(err)
}

if err := setTeam(ctx, d, team); err != nil {
return diag.FromErr(err)
}

log.Printf("[INFO] team read (%s)", d.Id())
return nil
}

func resourceTeamUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] updating team (%s)", d.Id())

team := expandTeam(d)
client := m.(*octopusdeploy.Client)
updatedTeam, err := client.Teams.Update(team)
if err != nil {
return diag.FromErr(err)
}

if err := setTeam(ctx, d, updatedTeam); err != nil {
return diag.FromErr(err)
}

log.Printf("[INFO] team updated (%s)", d.Id())
return nil
}
73 changes: 73 additions & 0 deletions octopusdeploy/schema_external_security_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package octopusdeploy

import (
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func expandExternalSecurityGroups(externalSecurityGroups []interface{}) []octopusdeploy.NamedReferenceItem {
expandedExternalSecurityGroups := make([]octopusdeploy.NamedReferenceItem, 0, len(externalSecurityGroups))
for _, externalSecurityGroup := range externalSecurityGroups {
if externalSecurityGroup != nil {
rawExternalSecurityGroup := externalSecurityGroup.(map[string]interface{})

displayIDAndName := false
if rawExternalSecurityGroup["display_id_and_name"] != nil {
displayIDAndName = rawExternalSecurityGroup["display_id_and_name"].(bool)
}

displayName := ""
if rawExternalSecurityGroup["display_name"] != nil {
displayName = rawExternalSecurityGroup["display_name"].(string)
}

id := ""
if rawExternalSecurityGroup["id"] != nil {
id = rawExternalSecurityGroup["id"].(string)
}

item := octopusdeploy.NamedReferenceItem{
DisplayIDAndName: displayIDAndName,
DisplayName: displayName,
ID: id,
}
expandedExternalSecurityGroups = append(expandedExternalSecurityGroups, item)
}
}
return expandedExternalSecurityGroups
}

func flattenExternalSecurityGroups(externalSecurityGroups []octopusdeploy.NamedReferenceItem) []interface{} {
if externalSecurityGroups == nil {
return nil
}

flattenedExternalSecurityGroups := make([]interface{}, len(externalSecurityGroups))
for i, externalSecurityGroup := range externalSecurityGroups {
rawExternalSecurityGroup := map[string]interface{}{
"display_id_and_name": externalSecurityGroup.DisplayIDAndName,
"display_name": externalSecurityGroup.DisplayName,
"id": externalSecurityGroup.ID,
}

flattenedExternalSecurityGroups[i] = rawExternalSecurityGroup
}

return flattenedExternalSecurityGroups
}

func getExternalSecurityGroupsSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"display_id_and_name": {
Computed: true,
Optional: true,
Type: schema.TypeBool,
},
"display_name": {
Computed: true,
Optional: true,
Type: schema.TypeString,
},
"id": getIDSchema(),
}
}
8 changes: 8 additions & 0 deletions octopusdeploy/schema_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ func getQueryIDs() *schema.Schema {
}
}

func getQueryIncludeSystem() *schema.Schema {
return &schema.Schema{
Description: "A filter to include system teams.",
Optional: true,
Type: schema.TypeBool,
}
}

func getQueryIsClone() *schema.Schema {
return &schema.Schema{
Description: "A filter to search for cloned resources.",
Expand Down
Loading

0 comments on commit 5d96f7f

Please sign in to comment.