forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_organization.go
150 lines (122 loc) · 5.14 KB
/
admin_organization.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package tfe
import (
"context"
"fmt"
"net/url"
)
// Compile-time proof of interface implementation.
var _ AdminOrganizations = (*adminOrganizations)(nil)
// AdminOrganizations describes all of the admin organization related methods that the Terraform
// Enterprise API supports. Note that admin settings are only available in Terraform Enterprise.
//
// TFE API docs: https://www.terraform.io/docs/cloud/api/admin/organizations.html
type AdminOrganizations interface {
// List all the organizations visible to the current user.
List(ctx context.Context, options AdminOrganizationListOptions) (*AdminOrganizationList, error)
// Read attributes of an existing organization via admin API.
Read(ctx context.Context, organization string) (*AdminOrganization, error)
// Update attributes of an existing organization via admin API.
Update(ctx context.Context, organization string, options AdminOrganizationUpdateOptions) (*AdminOrganization, error)
// Delete an organization by its name via admin API
Delete(ctx context.Context, organization string) error
}
// adminOrganizations implements AdminOrganizations.
type adminOrganizations struct {
client *Client
}
// AdminOrganization represents a Terraform Enterprise organization returned from the Admin API.
type AdminOrganization struct {
Name string `jsonapi:"primary,organizations"`
AccessBetaTools bool `jsonapi:"attr,access-beta-tools"`
ExternalID string `jsonapi:"attr,external-id"`
IsDisabled bool `jsonapi:"attr,is-disabled"`
NotificationEmail string `jsonapi:"attr,notification-email"`
SsoEnabled bool `jsonapi:"attr,sso-enabled"`
TerraformBuildWorkerApplyTimeout string `jsonapi:"attr,terraform-build-worker-apply-timeout"`
TerraformBuildWorkerPlanTimeout string `jsonapi:"attr,terraform-build-worker-plan-timeout"`
TerraformWorkerSudoEnabled bool `jsonapi:"attr,terraform-worker-sudo-enabled"`
// Relations
Owners []*User `jsonapi:"relation,owners"`
}
// AdminOrganizationUpdateOptions represents the admin options for updating an organization.
// https://www.terraform.io/docs/cloud/api/admin/organizations.html#request-body
type AdminOrganizationUpdateOptions struct {
AccessBetaTools *bool `jsonapi:"attr,access-beta-tools,omitempty"`
IsDisabled *bool `jsonapi:"attr,is-disabled,omitempty"`
TerraformBuildWorkerApplyTimeout *string `jsonapi:"attr,terraform-build-worker-apply-timeout,omitempty"`
TerraformBuildWorkerPlanTimeout *string `jsonapi:"attr,terraform-build-worker-plan-timeout,omitempty"`
TerraformWorkerSudoEnabled bool `jsonapi:"attr,terraform-worker-sudo-enabled,omitempty"`
}
// AdminOrganizationList represents a list of organizations via Admin API.
type AdminOrganizationList struct {
*Pagination
Items []*AdminOrganization
}
// AdminOrganizationListOptions represents the options for listing organizations via Admin API.
type AdminOrganizationListOptions struct {
ListOptions
// A query string used to filter organizations.
// Any organizations with a name or notification email partially matching this value will be returned.
Query *string `url:"q,omitempty"`
// A list of relations to include. See available resources
// https://www.terraform.io/docs/cloud/api/admin/organizations.html#available-related-resources
Include *string `url:"include"`
}
// List all the organizations visible to the current user.
func (s *adminOrganizations) List(ctx context.Context, options AdminOrganizationListOptions) (*AdminOrganizationList, error) {
url := "admin/organizations"
req, err := s.client.newRequest("GET", url, &options)
if err != nil {
return nil, err
}
orgl := &AdminOrganizationList{}
err = s.client.do(ctx, req, orgl)
if err != nil {
return nil, err
}
return orgl, nil
}
func (s *adminOrganizations) Read(ctx context.Context, organization string) (*AdminOrganization, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
u := fmt.Sprintf("admin/organizations/%s", url.QueryEscape(organization))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
org := &AdminOrganization{}
err = s.client.do(ctx, req, org)
if err != nil {
return nil, err
}
return org, nil
}
func (s *adminOrganizations) Update(ctx context.Context, organization string, options AdminOrganizationUpdateOptions) (*AdminOrganization, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
u := fmt.Sprintf("admin/organizations/%s", url.QueryEscape(organization))
req, err := s.client.newRequest("PATCH", u, &options)
if err != nil {
return nil, err
}
org := &AdminOrganization{}
err = s.client.do(ctx, req, org)
if err != nil {
return nil, err
}
return org, nil
}
// Delete an organization by its name.
func (s *adminOrganizations) Delete(ctx context.Context, organization string) error {
if !validStringID(&organization) {
return ErrInvalidOrg
}
u := fmt.Sprintf("admin/organizations/%s", url.QueryEscape(organization))
req, err := s.client.newRequest("DELETE", u, nil)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}