-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port 4645 terraform provider add support in team resource (#74)
- Loading branch information
Showing
12 changed files
with
551 additions
and
2 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
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,34 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "port_team Resource - terraform-provider-port-labs" | ||
subcategory: "" | ||
description: |- | ||
Team resource | ||
--- | ||
|
||
# port_team (Resource) | ||
|
||
Team resource | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) The name of the team | ||
|
||
### Optional | ||
|
||
- `description` (String) The description of the team | ||
- `users` (List of String) The users of the team | ||
|
||
### Read-Only | ||
|
||
- `created_at` (String) The creation date of the team | ||
- `id` (String) The ID of this resource. | ||
- `provider_name` (String) The provider of the team | ||
- `updated_at` (String) The last update date of the team | ||
|
||
|
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,9 @@ | ||
resource "port_team" "example" { | ||
name = "example" | ||
description = "example" | ||
users = [ | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
] | ||
} |
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,109 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
func (c *PortClient) ReadTeam(ctx context.Context, teamName string) (*Team, int, error) { | ||
url := "v1/teams/{name}?fields=name&fields=provider&fields=description&fields=createdAt&fields=updatedAt&fields=users.firstName&fields=users.status&fields=users.email" | ||
resp, err := c.Client.R(). | ||
SetContext(ctx). | ||
SetHeader("Accept", "application/json"). | ||
SetPathParam("name", teamName). | ||
Get(url) | ||
if err != nil { | ||
return nil, resp.StatusCode(), err | ||
} | ||
|
||
var pt PortTeamBody | ||
err = json.Unmarshal(resp.Body(), &pt) | ||
if err != nil { | ||
return nil, resp.StatusCode(), err | ||
} | ||
|
||
if !pt.OK { | ||
return nil, resp.StatusCode(), fmt.Errorf("failed to read team, got: %s", resp.Body()) | ||
} | ||
team := &Team{ | ||
Name: pt.Team.Name, | ||
Description: pt.Team.Description, | ||
CreatedAt: pt.Team.CreatedAt, | ||
UpdatedAt: pt.Team.UpdatedAt, | ||
} | ||
|
||
team.Users = make([]string, len(pt.Team.Users)) | ||
|
||
for i, u := range pt.Team.Users { | ||
team.Users[i] = u.Email | ||
} | ||
|
||
return team, resp.StatusCode(), nil | ||
} | ||
|
||
func (c *PortClient) CreateTeam(ctx context.Context, team *Team) (*Team, error) { | ||
url := "v1/teams" | ||
resp, err := c.Client.R(). | ||
SetBody(team). | ||
SetContext(ctx). | ||
Post(url) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
var pb PortBody | ||
err = json.Unmarshal(resp.Body(), &pb) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !pb.OK { | ||
return nil, fmt.Errorf("failed to create team, got: %s", resp.Body()) | ||
} | ||
return &pb.Team, nil | ||
} | ||
|
||
func (c *PortClient) UpdateTeam(ctx context.Context, teamName string, team *Team) (*Team, error) { | ||
url := "v1/teams/{name}" | ||
resp, err := c.Client.R(). | ||
SetBody(team). | ||
SetContext(ctx). | ||
SetPathParam("name", teamName). | ||
Patch(url) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
var pb PortBody | ||
err = json.Unmarshal(resp.Body(), &pb) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !pb.OK { | ||
return nil, fmt.Errorf("failed to update team, got: %s", resp.Body()) | ||
} | ||
return &pb.Team, nil | ||
} | ||
|
||
func (c *PortClient) DeleteTeam(ctx context.Context, teamName string) error { | ||
url := "v1/teams/{name}" | ||
resp, err := c.Client.R(). | ||
SetContext(ctx). | ||
SetHeader("Accept", "application/json"). | ||
SetPathParam("name", teamName). | ||
Delete(url) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
var pb PortBodyDelete | ||
err = json.Unmarshal(resp.Body(), &pb) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !(pb.Ok) { | ||
return fmt.Errorf("failed to delete team. got:\n%s", string(resp.Body())) | ||
} | ||
return nil | ||
} |
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,15 @@ | ||
package team | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
type TeamModel struct { | ||
ID types.String `tfsdk:"id"` | ||
Name types.String `tfsdk:"name"` | ||
Description types.String `tfsdk:"description"` | ||
Users []types.String `tfsdk:"users"` | ||
CreatedAt types.String `tfsdk:"created_at"` | ||
UpdatedAt types.String `tfsdk:"updated_at"` | ||
ProviderName types.String `tfsdk:"provider_name"` | ||
} |
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,26 @@ | ||
package team | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/port-labs/terraform-provider-port-labs/internal/cli" | ||
"github.com/port-labs/terraform-provider-port-labs/internal/flex" | ||
) | ||
|
||
func refreshTeamState(ctx context.Context, state *TeamModel, t *cli.Team) error { | ||
state.CreatedAt = types.StringValue(t.CreatedAt.String()) | ||
state.UpdatedAt = types.StringValue(t.UpdatedAt.String()) | ||
state.ID = types.StringValue(t.Name) | ||
state.Name = types.StringValue(t.Name) | ||
state.Description = flex.GoStringToFramework(&t.Description) | ||
|
||
if len(t.Users) != 0 { | ||
state.Users = make([]types.String, len(t.Users)) | ||
for i, u := range t.Users { | ||
state.Users[i] = types.StringValue(u) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.