-
Notifications
You must be signed in to change notification settings - Fork 24
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
Showing
14 changed files
with
1,083 additions
and
29 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,174 @@ | ||
package cfclient | ||
|
||
import ( | ||
"fmt" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
type ServiceUserTeam struct { | ||
ID string `json:"_id,omitempty"` | ||
} | ||
|
||
type ServiceUser struct { | ||
ID string `json:"_id,omitempty"` | ||
Name string `json:"userName,omitempty"` | ||
Teams []ServiceUserTeam `json:"teams,omitempty"` | ||
Roles []string `json:"roles,omitempty"` | ||
} | ||
|
||
type ServiceUserCreateUpdate struct { | ||
ID string `json:"_id,omitempty"` | ||
Name string `json:"userName,omitempty"` | ||
TeamIDs []string `json:"teamIds,omitempty"` | ||
AssignAdminRole bool `json:"assignAdminRole,omitempty"` | ||
} | ||
|
||
// GetID implement CodefreshObject interface | ||
func (serviceuser *ServiceUser) GetID() string { | ||
return serviceuser.ID | ||
} | ||
|
||
func (serviceuser *ServiceUser) HasAdminRole() bool { | ||
return slices.Contains(serviceuser.Roles, "Admin") | ||
} | ||
|
||
func (client *Client) GetServiceUserList() ([]ServiceUser, error) { | ||
fullPath := "/service-users" | ||
opts := RequestOptions{ | ||
Path: fullPath, | ||
Method: "GET", | ||
} | ||
|
||
resp, err := client.RequestAPI(&opts) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var serviceusers []ServiceUser | ||
|
||
err = DecodeResponseInto(resp, &serviceusers) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return serviceusers, nil | ||
} | ||
|
||
func (client *Client) GetServiceUserByName(name string) (*ServiceUser, error) { | ||
|
||
serviceusers, err := client.GetServiceUserList() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, serviceuser := range serviceusers { | ||
if serviceuser.Name == name { | ||
return &serviceuser, nil | ||
} | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func (client *Client) GetServiceUserByID(id string) (*ServiceUser, error) { | ||
|
||
fullPath := fmt.Sprintf("/service-users/%s", id) | ||
opts := RequestOptions{ | ||
Path: fullPath, | ||
Method: "GET", | ||
} | ||
|
||
resp, err := client.RequestAPI(&opts) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var serviceuser ServiceUser | ||
|
||
err = DecodeResponseInto(resp, &serviceuser) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &serviceuser, nil | ||
} | ||
|
||
func (client *Client) CreateServiceUser(serviceUserCreateUpdate *ServiceUserCreateUpdate) (*ServiceUser, error) { | ||
|
||
fullPath := "/service-users" | ||
body, err := EncodeToJSON(serviceUserCreateUpdate) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
opts := RequestOptions{ | ||
Path: fullPath, | ||
Method: "POST", | ||
Body: body, | ||
} | ||
|
||
resp, err := client.RequestAPI(&opts) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var serviceuser ServiceUser | ||
|
||
err = DecodeResponseInto(resp, &serviceuser) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &serviceuser, nil | ||
} | ||
|
||
func (client *Client) UpdateServiceUser(serviceUserCreateUpdate *ServiceUserCreateUpdate) (*ServiceUser, error) { | ||
|
||
fullPath := fmt.Sprintf("/service-users/%s", serviceUserCreateUpdate.ID) | ||
body, err := EncodeToJSON(serviceUserCreateUpdate) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
opts := RequestOptions{ | ||
Path: fullPath, | ||
Method: "PATCH", | ||
Body: body, | ||
} | ||
|
||
resp, err := client.RequestAPI(&opts) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var serviceuser ServiceUser | ||
|
||
err = DecodeResponseInto(resp, &serviceuser) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &serviceuser, nil | ||
} | ||
|
||
func (client *Client) DeleteServiceUser(id string) error { | ||
fullPath := fmt.Sprintf("/service-users/%s", id) | ||
opts := RequestOptions{ | ||
Path: fullPath, | ||
Method: "DELETE", | ||
} | ||
|
||
_, err := client.RequestAPI(&opts) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
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,85 @@ | ||
package codefresh | ||
|
||
import ( | ||
"fmt" | ||
|
||
cfClient "github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceServiceAccount() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "This data source retrieves a Codefresh service account by its ID or name.", | ||
Read: dataSourceServiceAccountRead, | ||
Schema: map[string]*schema.Schema{ | ||
"_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"name": { | ||
Description: "Service account name", | ||
Type: schema.TypeString, | ||
Optional: true, | ||
AtLeastOneOf: []string{"_id", "name"}, | ||
}, | ||
"assign_admin_role": { | ||
Description: "Whether or not account admin role is assigned to the service account", | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
}, | ||
"assigned_teams": { | ||
Description: "A list of team IDs the service account is be assigned to", | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceServiceAccountRead(d *schema.ResourceData, meta interface{}) error { | ||
|
||
client := meta.(*cfClient.Client) | ||
var serviceAccount *cfClient.ServiceUser | ||
var err error | ||
|
||
if _id, _idOk := d.GetOk("_id"); _idOk { | ||
serviceAccount, err = client.GetServiceUserByID(_id.(string)) | ||
} else if name, nameOk := d.GetOk("name"); nameOk { | ||
serviceAccount, err = client.GetServiceUserByName(name.(string)) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if serviceAccount == nil { | ||
return fmt.Errorf("data.codefresh_service_account - cannot find service account") | ||
} | ||
|
||
return mapDataServiceAccountToResource(serviceAccount, d) | ||
|
||
} | ||
|
||
func mapDataServiceAccountToResource(serviceAccount *cfClient.ServiceUser, d *schema.ResourceData) error { | ||
|
||
if serviceAccount == nil || serviceAccount.ID == "" { | ||
return fmt.Errorf("data.codefresh_service_account - failed to mapDataServiceAccountToResource") | ||
} | ||
|
||
d.SetId(serviceAccount.ID) | ||
d.Set("name", serviceAccount.Name) | ||
d.Set("assign_admin_role", serviceAccount.HasAdminRole()) | ||
|
||
teamIds := []string{} | ||
|
||
for _, team := range serviceAccount.Teams { | ||
teamIds = append(teamIds, team.ID) | ||
} | ||
|
||
d.Set("assigned_teams", teamIds) | ||
|
||
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
Oops, something went wrong.