Skip to content

Commit

Permalink
Add team_user resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawn Castrianni authored and Shawn Castrianni committed Jun 4, 2023
1 parent 9f60c0c commit 31f2a1e
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 7 deletions.
23 changes: 23 additions & 0 deletions docs/resources/team_user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Resource: konnect_team_user
Represents a member of a team
## Example usage
```hcl
resource "konnect_team" "Team" {
name = "Test"
description = "testing"
}
data "konnect_user" "User" {
search_full_name = "Joe"
}
resource "konnect_team_user" "example" {
team_id = konnect_team.Team.id
user_id = data.konnect_user.User.id
}
```
## Argument Reference
* `team_id` - **(Required, ForceNew, String)** The id of the team.
* `user_id` - **(Required, ForceNew, String)** The id of the user.
## Attribute Reference
* `id` - **(String)** Same as `team_id`:`user_id`
## Import
Team users can be imported using a proper value of `id` as described above
1 change: 1 addition & 0 deletions konnect/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
Bearer = "Bearer"
KonnectDomain = "api.konghq.com"
GlobalRegion = "global"
IdSeparator = ":"
FilterName = "filter[name]"
FilterNameContains = "filter[name][contains]"
FilterFullName = "filter[full_name]"
Expand Down
23 changes: 23 additions & 0 deletions konnect/client/team_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package client

import "strings"

const (
TeamUserPath = "teams/%s/users"
TeamUserPathCreate = TeamUserPath
TeamUserPathDelete = TeamUserPath + "/%s"
)

type TeamUser struct {
TeamId string `json:"-"`
UserId string `json:"id"`
}

func (tm *TeamUser) TeamUserEncodeId() string {
return tm.TeamId + IdSeparator + tm.UserId
}

func TeamUserDecodeId(s string) (string, string) {
tokens := strings.Split(s, IdSeparator)
return tokens[0], tokens[1]
}
1 change: 1 addition & 0 deletions konnect/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func Provider() *schema.Provider {
"konnect_identity_provider": resourceIdentityProvider(),
"konnect_user": resourceUser(),
"konnect_team": resourceTeam(),
"konnect_team_user": resourceTeamUser(),
},
DataSourcesMap: map[string]*schema.Resource{
"konnect_runtime_group": dataSourceRuntimeGroup(),
Expand Down
120 changes: 120 additions & 0 deletions konnect/resource_team_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package konnect

import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/go-http-utils/headers"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scastria/terraform-provider-konnect/konnect/client"
"net/http"
)

func resourceTeamUser() *schema.Resource {
return &schema.Resource{
CreateContext: resourceTeamUserCreate,
ReadContext: resourceTeamUserRead,
DeleteContext: resourceTeamUserDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"team_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"user_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func fillTeamUser(c *client.TeamUser, d *schema.ResourceData) {
c.TeamId = d.Get("team_id").(string)
c.UserId = d.Get("user_id").(string)
}

func fillResourceDataFromTeamUser(c *client.TeamUser, d *schema.ResourceData) {
d.Set("team_id", c.TeamId)
d.Set("user_id", c.UserId)
}

func resourceTeamUserCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
c := m.(*client.Client)
buf := bytes.Buffer{}
newTeamUser := client.TeamUser{}
fillTeamUser(&newTeamUser, d)
err := json.NewEncoder(&buf).Encode(newTeamUser)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
requestPath := fmt.Sprintf(client.TeamUserPathCreate, newTeamUser.TeamId)
requestHeaders := http.Header{
headers.ContentType: []string{client.ApplicationJson},
}
_, err = c.HttpRequest(ctx, false, http.MethodPost, requestPath, nil, requestHeaders, &buf)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
d.SetId(newTeamUser.TeamUserEncodeId())
return diags
}

func resourceTeamUserRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
teamId, userId := client.TeamUserDecodeId(d.Id())
c := m.(*client.Client)
requestPath := fmt.Sprintf(client.TeamUserPath, teamId)
body, err := c.HttpRequest(ctx, false, http.MethodGet, requestPath, nil, nil, &bytes.Buffer{})
if err != nil {
d.SetId("")
re := err.(*client.RequestError)
if re.StatusCode == http.StatusNotFound {
return diags
}
return diag.FromErr(err)
}
retVal := &client.UserCollection{}
err = json.NewDecoder(body).Decode(retVal)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
//Search users looking for userId
foundUser := false
for _, u := range retVal.Users {
if u.Id == userId {
foundUser = true
break
}
}
if !foundUser {
d.SetId("")
return diags
}
d.Set("team_id", teamId)
d.Set("user_id", userId)
return diags
}

func resourceTeamUserDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
teamId, userId := client.TeamUserDecodeId(d.Id())
c := m.(*client.Client)
requestPath := fmt.Sprintf(client.TeamUserPathDelete, teamId, userId)
_, err := c.HttpRequest(ctx, false, http.MethodDelete, requestPath, nil, nil, &bytes.Buffer{})
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return diags
}
19 changes: 12 additions & 7 deletions test/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ terraform {
provider "konnect" {
}

#resource "konnect_team" "T" {
# name = "ShawnTest"
# description = "testing"
#}
resource "konnect_team" "T" {
name = "ShawnTest"
description = "testing"
}

#data "konnect_team" "T" {
# search_name = "eve"
Expand All @@ -24,9 +24,14 @@ provider "konnect" {
# preferred_name = "Joe"
#}

#data "konnect_user" "U" {
# full_name = "Joe Blow"
#}
data "konnect_user" "U" {
search_full_name = "Julia"
}

resource "konnect_team_user" "TU" {
team_id = konnect_team.T.id
user_id = data.konnect_user.U.id
}

#resource "konnect_identity_provider" "IP" {
# issuer = "https://greenst.okta.com/oauth2/default"
Expand Down

0 comments on commit 31f2a1e

Please sign in to comment.