|
1 | 1 | package types |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/google/uuid" |
5 | 4 | "github.com/zclconf/go-cty/cty" |
6 | 5 | "github.com/zclconf/go-cty/cty/gocty" |
| 6 | + "golang.org/x/xerrors" |
7 | 7 | ) |
8 | 8 |
|
9 | 9 | // Based on https://github.com/coder/terraform-provider-coder/blob/9a745586b23a9cb5de2f65a2dcac12e48b134ffa/provider/workspace_owner.go#L72 |
10 | 10 | type WorkspaceOwner struct { |
11 | | - ID uuid.UUID `json:"id"` |
12 | | - Name string `json:"name"` |
13 | | - FullName string `json:"full_name"` |
14 | | - Email string `json:"email"` |
15 | | - SSHPublicKey string `json:"ssh_public_key"` |
| 11 | + ID string `json:"id" cty:"id"` |
| 12 | + Name string `json:"name" cty:"name"` |
| 13 | + FullName string `json:"full_name" cty:"full_name"` |
| 14 | + Email string `json:"email" cty:"email"` |
| 15 | + SSHPublicKey string `json:"ssh_public_key" cty:"ssh_public_key"` |
16 | 16 | // SSHPrivateKey is intentionally omitted for now, due to the security risk |
17 | 17 | // that exposing it poses. |
18 | | - // SSHPrivateKey string `json:"ssh_private_key"` |
19 | | - Groups []string `json:"groups"` |
| 18 | + // SSHPrivateKey string `json:"ssh_private_key" cty:"ssh_private_key"` |
| 19 | + Groups []string `json:"groups" cty:"groups"` |
20 | 20 | // SessionToken is intentionally omitted for now, due to the security risk |
21 | 21 | // that exposing it poses. |
22 | | - // SessionToken string `json:"session_token"` |
| 22 | + // SessionToken string `json:"session_token" cty:"session_token"` |
23 | 23 | // OIDCAccessToken is intentionally omitted for now, due to the security risk |
24 | 24 | // that exposing it poses. |
25 | | - // OIDCAccessToken string `json:"oidc_access_token"` |
26 | | - LoginType string `json:"login_type"` |
27 | | - RBACRoles []WorkspaceOwnerRBACRole `json:"rbac_roles"` |
| 25 | + // OIDCAccessToken string `json:"oidc_access_token" cty:"oidc_access_token"` |
| 26 | + LoginType string `json:"login_type" cty:"login_type"` |
| 27 | + RBACRoles []WorkspaceOwnerRBACRole `json:"rbac_roles" cty:"rbac_roles"` |
| 28 | +} |
| 29 | + |
| 30 | +type WorkspaceOwnerRBACRole struct { |
| 31 | + Name string `json:"name" cty:"name"` |
| 32 | + OrgID string `json:"org_id" cty:"org_id"` |
28 | 33 | } |
29 | 34 |
|
30 | 35 | func (o *WorkspaceOwner) ToCtyValue() (cty.Value, error) { |
31 | 36 | if o.Groups == nil { |
32 | | - o.Groups = []string{} |
| 37 | + o.Groups = make([]string, 0) |
33 | 38 | } |
34 | | - convertedGroups, err := gocty.ToCtyValue(o.Groups, cty.List(cty.String)) |
35 | | - if err != nil { |
36 | | - return cty.Value{}, err |
| 39 | + if o.RBACRoles == nil { |
| 40 | + o.RBACRoles = make([]WorkspaceOwnerRBACRole, 0) |
37 | 41 | } |
38 | 42 |
|
39 | | - roleValues := make([]cty.Value, 0, len(o.RBACRoles)) |
40 | | - for _, role := range o.RBACRoles { |
41 | | - roleValue, err := role.ToCtyValue() |
42 | | - if err != nil { |
43 | | - return cty.Value{}, err |
44 | | - } |
45 | | - roleValues = append(roleValues, roleValue) |
46 | | - } |
47 | | - var convertedRoles cty.Value = cty.ListValEmpty(WorkspaceOwnerRBACRole{}.CtyType()) |
48 | | - if len(roleValues) > 0 { |
49 | | - convertedRoles = cty.ListVal(roleValues) |
| 43 | + ownerValue, err := gocty.ToCtyValue(o, cty.Object(map[string]cty.Type{ |
| 44 | + "id": cty.String, |
| 45 | + "name": cty.String, |
| 46 | + "full_name": cty.String, |
| 47 | + "email": cty.String, |
| 48 | + "ssh_public_key": cty.String, |
| 49 | + "groups": cty.List(cty.String), |
| 50 | + "login_type": cty.String, |
| 51 | + "rbac_roles": cty.List(cty.Object( |
| 52 | + map[string]cty.Type{ |
| 53 | + "name": cty.String, |
| 54 | + "org_id": cty.String, |
| 55 | + }, |
| 56 | + )), |
| 57 | + })) |
| 58 | + if err != nil { |
| 59 | + return cty.Value{}, xerrors.Errorf("failed to convert owner value", err) |
50 | 60 | } |
51 | | - |
52 | | - return cty.ObjectVal(map[string]cty.Value{ |
53 | | - "id": cty.StringVal(o.ID.String()), |
54 | | - "name": cty.StringVal(o.Name), |
55 | | - "full_name": cty.StringVal(o.FullName), |
56 | | - "email": cty.StringVal(o.Email), |
57 | | - "ssh_public_key": cty.StringVal(o.SSHPublicKey), |
58 | | - "groups": convertedGroups, |
59 | | - "login_type": cty.StringVal(o.LoginType), |
60 | | - "rbac_roles": convertedRoles, |
61 | | - }), nil |
62 | | -} |
63 | | - |
64 | | -type WorkspaceOwnerRBACRole struct { |
65 | | - Name string `json:"name"` |
66 | | - OrgID uuid.UUID `json:"org_id"` |
67 | | -} |
68 | | - |
69 | | -func (_ WorkspaceOwnerRBACRole) CtyType() cty.Type { |
70 | | - return cty.Object(map[string]cty.Type{ |
71 | | - "name": cty.String, |
72 | | - "org_id": cty.String, |
73 | | - }) |
74 | | -} |
75 | | - |
76 | | -func (r *WorkspaceOwnerRBACRole) ToCtyValue() (cty.Value, error) { |
77 | | - return cty.ObjectVal(map[string]cty.Value{ |
78 | | - "name": cty.StringVal(r.Name), |
79 | | - "org_id": cty.StringVal(r.OrgID.String()), |
80 | | - }), nil |
| 61 | + return ownerValue, nil |
81 | 62 | } |
0 commit comments