-
Notifications
You must be signed in to change notification settings - Fork 102
/
admin_workspace.go
148 lines (118 loc) · 4.5 KB
/
admin_workspace.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"context"
"fmt"
"net/url"
)
// Compile-time proof of interface implementation.
var _ AdminWorkspaces = (*adminWorkspaces)(nil)
// AdminWorkspaces describes all the admin workspace related methods that the Terraform Enterprise API supports.
// Note that admin settings are only available in Terraform Enterprise.
//
// TFE API docs: https://developer.hashicorp.com/terraform/enterprise/api-docs/admin/workspaces
type AdminWorkspaces interface {
// List all the workspaces within a workspace.
List(ctx context.Context, options *AdminWorkspaceListOptions) (*AdminWorkspaceList, error)
// Read a workspace by its ID.
Read(ctx context.Context, workspaceID string) (*AdminWorkspace, error)
// Delete a workspace by its ID.
Delete(ctx context.Context, workspaceID string) error
}
// adminWorkspaces implements AdminWorkspaces interface.
type adminWorkspaces struct {
client *Client
}
// AdminVCSRepo represents a VCS repository
type AdminVCSRepo struct {
Identifier string `jsonapi:"attr,identifier"`
}
// AdminWorkspaces represents a Terraform Enterprise admin workspace.
type AdminWorkspace struct {
ID string `jsonapi:"primary,workspaces"`
Name string `jsonapi:"attr,name"`
Locked bool `jsonapi:"attr,locked"`
VCSRepo *AdminVCSRepo `jsonapi:"attr,vcs-repo"`
// Relations
Organization *Organization `jsonapi:"relation,organization"`
CurrentRun *Run `jsonapi:"relation,current-run"`
}
// AdminWorkspaceIncludeOpt represents the available options for include query params.
// https://developer.hashicorp.com/terraform/enterprise/api-docs/admin/workspaces#available-related-resources
type AdminWorkspaceIncludeOpt string
const (
AdminWorkspaceOrg AdminWorkspaceIncludeOpt = "organization"
AdminWorkspaceCurrentRun AdminWorkspaceIncludeOpt = "current_run"
AdminWorkspaceOrgOwners AdminWorkspaceIncludeOpt = "organization.owners"
)
// AdminWorkspaceListOptions represents the options for listing workspaces.
type AdminWorkspaceListOptions struct {
ListOptions
// A query string (partial workspace name) used to filter the results.
// https://developer.hashicorp.com/terraform/enterprise/api-docs/admin/workspaces#query-parameters
Query string `url:"q,omitempty"`
// Optional: A list of relations to include. See available resources
// https://developer.hashicorp.com/terraform/enterprise/api-docs/admin/workspaces#available-related-resources
Include []AdminWorkspaceIncludeOpt `url:"include,omitempty"`
// Optional: A comma-separated list of Run statuses to restrict results. See available resources
// https://developer.hashicorp.com/terraform/enterprise/api-docs/admin/workspaces#query-parameters
Filter string `url:"filter[current_run][status],omitempty"`
// Optional: May sort on "name" (the default) and "current-run.created-at" (which sorts by the time of the current run)
// Prepending a hyphen to the sort parameter will reverse the order (e.g. "-name" to reverse the default order)
Sort string `url:"sort,omitempty"`
}
// AdminWorkspaceList represents a list of workspaces.
type AdminWorkspaceList struct {
*Pagination
Items []*AdminWorkspace
}
// List all the workspaces within a workspace.
func (s *adminWorkspaces) List(ctx context.Context, options *AdminWorkspaceListOptions) (*AdminWorkspaceList, error) {
if err := options.valid(); err != nil {
return nil, err
}
u := "admin/workspaces"
req, err := s.client.NewRequest("GET", u, options)
if err != nil {
return nil, err
}
awl := &AdminWorkspaceList{}
err = req.Do(ctx, awl)
if err != nil {
return nil, err
}
return awl, nil
}
// Read a workspace by its ID.
func (s *adminWorkspaces) Read(ctx context.Context, workspaceID string) (*AdminWorkspace, error) {
if !validStringID(&workspaceID) {
return nil, ErrInvalidWorkspaceValue
}
u := fmt.Sprintf("admin/workspaces/%s", url.PathEscape(workspaceID))
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
aw := &AdminWorkspace{}
err = req.Do(ctx, aw)
if err != nil {
return nil, err
}
return aw, nil
}
// Delete a workspace by its ID.
func (s *adminWorkspaces) Delete(ctx context.Context, workspaceID string) error {
if !validStringID(&workspaceID) {
return ErrInvalidWorkspaceValue
}
u := fmt.Sprintf("admin/workspaces/%s", url.PathEscape(workspaceID))
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return err
}
return req.Do(ctx, nil)
}
func (o *AdminWorkspaceListOptions) valid() error {
return nil
}