Skip to content

Commit

Permalink
Add team.go
Browse files Browse the repository at this point in the history
  • Loading branch information
palson-cf committed Jul 9, 2020
1 parent dfc2ca1 commit a24bec6
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 1 deletion.
250 changes: 250 additions & 0 deletions client/team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
package client

import (
"fmt"
)

type TeamUser struct {
ID string `json:"id,omitempty"`
UserName string `json:"userName,omitempty"`
}

// Team spec
type Team struct {
ID string `json:"_id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Account string `json:"account,omitempty"`
Tags []string `json:"tags,omitempty"`
Users []TeamUser `json:"users,omitempty"`
}

// NewTeam Codefresh API expects a list of users IDs to create a new team
type NewTeam struct {
ID string `json:"_id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Account string `json:"account,omitempty"`
Tags []string `json:"tags,omitempty"`
Users []string `json:"users,omitempty"`
}

// GetID implement CodefreshObject interface
func (team *Team) GetID() string {
return team.ID
}

func (client *Client) GetTeamList() ([]Team, error) {
fullPath := "/team"
opts := RequestOptions{
Path: fullPath,
Method: "GET",
}

resp, err := client.RequestAPI(&opts)

if err != nil {
return nil, err
}

var teams []Team

err = DecodeResponseInto(resp, &teams)
if err != nil {
return nil, err
}

return teams, nil
}

func (client *Client) GetTeamByName(name string) (*Team, error) {

teams, err := client.GetTeamList()
if err != nil {
return nil, err
}

for _, team := range teams {
if team.Name == name {
return &team, nil
}
}

return nil, nil
}

func (client *Client) GetTeamByID(id string) (*Team, error) {

teams, err := client.GetTeamList()
if err != nil {
return nil, err
}

for _, team := range teams {
if team.ID == id {
return &team, nil
}
}

return nil, nil
}

//
func ConvertToNewTeam(team *Team) *NewTeam {
var users []string

for _, user := range team.Users {
users = append(users, user.ID)
}

return &NewTeam{
ID: team.ID,
Name: team.Name,
Type: team.Type,
Account: team.Account,
Tags: team.Tags,
Users: users,
}
}

// CreateTeam POST team
func (client *Client) CreateTeam(team *Team) (*NewTeam, error) {

newTeam := ConvertToNewTeam(team)
body, err := EncodeToJSON(newTeam)

if err != nil {
return nil, err
}
opts := RequestOptions{
Path: "/team",
Method: "POST",
Body: body,
}

resp, err := client.RequestAPI(&opts)

if err != nil {
return nil, err
}

var respTeam NewTeam
err = DecodeResponseInto(resp, &respTeam)
if err != nil {
return nil, err
}

return &respTeam, nil
}

// DeleteTeam
func (client *Client) DeleteTeam(id string) error {
fullPath := fmt.Sprintf("/team/%s", id)
opts := RequestOptions{
Path: fullPath,
Method: "DELETE",
}

_, err := client.RequestAPI(&opts)

if err != nil {
return err
}

return nil
}

func (client *Client) SynchronizeClientWithGroup(name, ssoType string, notifications bool) error {

fullPath := fmt.Sprintf("/team/group/synchronize/name/%s/type/%s?disableNotifications=%b", name, ssoType, notifications)
opts := RequestOptions{
Path: fullPath,
Method: "GET",
}

_, err := client.RequestAPI(&opts)

if err != nil {
return err
}

return nil
}

func (client *Client) AddUserToTeam(teamID, userID string) (*Team, error) {

fullPath := fmt.Sprintf("/team/%s/%s/assignUserToTeam", teamID, userID)
opts := RequestOptions{
Path: fullPath,
Method: "PUT",
}

resp, err := client.RequestAPI(&opts)

if err != nil {
return nil, err
}

var respTeam Team
err = DecodeResponseInto(resp, &respTeam)
if err != nil {
return nil, err
}

return &respTeam, nil
}

func (client *Client) DeleteUserFromTeam(teamID, userID string) (*Team, error) {

fullPath := fmt.Sprintf("/team/%s/%s/deleteUserFromTeam", teamID, userID)
opts := RequestOptions{
Path: fullPath,
Method: "PUT",
}

resp, err := client.RequestAPI(&opts)

if err != nil {
return nil, err
}

var respTeam Team
err = DecodeResponseInto(resp, &respTeam)
if err != nil {
return nil, err
}

return &respTeam, nil
}

func (client *Client) RenameTeam(teamID, name string) (*Team, error) {

fullPath := fmt.Sprintf("/team/%s/renameTeam", teamID)

team := Team{Name: name}
body, err := EncodeToJSON(team)

if err != nil {
return nil, err
}

opts := RequestOptions{
Path: fullPath,
Method: "PUT",
Body: body,
}

resp, err := client.RequestAPI(&opts)

if err != nil {
return nil, err
}

var respTeam Team
err = DecodeResponseInto(resp, &respTeam)
if err != nil {
return nil, err
}

return &respTeam, nil
}
1 change: 1 addition & 0 deletions codefresh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func Provider() terraform.ResourceProvider {
"codefresh_project": resourceProject(),
"codefresh_pipeline": resourcePipeline(),
"codefresh_team": resourceTeam(),
"codefresh_account": resourceAccount(),
},
ConfigureFunc: configureProvider,
}
Expand Down
2 changes: 1 addition & 1 deletion codefresh/resource_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func resourceTeamUpdate(d *schema.ResourceData, meta interface{}) error {

team := *mapResourceToTeam(d)

_, err := client.CreateTeam(&team)
_, err := client.RenameTeam(team.ID, team.Name)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/golangci/golangci-lint v1.27.0
github.com/hashicorp/terraform v0.12.25
github.com/hashicorp/terraform-plugin-sdk v1.7.0
github.com/imdario/mergo v0.3.9
)

go 1.13
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a h1:GmsqmapfzSJkm28dhRoHz2tLRbJmqhU86IPgBtN3mmk=
Expand Down

0 comments on commit a24bec6

Please sign in to comment.