generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shawn Castrianni
authored and
Shawn Castrianni
committed
Jun 4, 2023
1 parent
9f60c0c
commit 31f2a1e
Showing
6 changed files
with
180 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters