|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 8 | +) |
| 9 | + |
| 10 | +type BranchMatcher struct { |
| 11 | + Pattern string `json:"pattern"` |
| 12 | + Type string `json:"type"` |
| 13 | +} |
| 14 | + |
| 15 | +type CreateCustomEnvironmentRequest struct { |
| 16 | + TeamID string `json:"-"` |
| 17 | + ProjectID string `json:"-"` |
| 18 | + Slug string `json:"slug"` |
| 19 | + Description string `json:"description"` |
| 20 | + BranchMatcher *BranchMatcher `json:"branchMatcher,omitempty"` |
| 21 | +} |
| 22 | + |
| 23 | +type CustomEnvironmentResponse struct { |
| 24 | + ID string `json:"id"` |
| 25 | + Description string `json:"description"` |
| 26 | + Slug string `json:"slug"` |
| 27 | + BranchMatcher *BranchMatcher `json:"branchMatcher"` |
| 28 | + TeamID string `json:"-"` |
| 29 | + ProjectID string `json:"-"` |
| 30 | +} |
| 31 | + |
| 32 | +func (c *Client) CreateCustomEnvironment(ctx context.Context, request CreateCustomEnvironmentRequest) (res CustomEnvironmentResponse, err error) { |
| 33 | + url := fmt.Sprintf("%s/v1/projects/%s/custom-environments", c.baseURL, request.ProjectID) |
| 34 | + if c.teamID(request.TeamID) != "" { |
| 35 | + url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID)) |
| 36 | + } |
| 37 | + payload := string(mustMarshal(request)) |
| 38 | + tflog.Info(ctx, "creating custom environment", map[string]interface{}{ |
| 39 | + "url": url, |
| 40 | + "payload": payload, |
| 41 | + }) |
| 42 | + err = c.doRequest(clientRequest{ |
| 43 | + ctx: ctx, |
| 44 | + method: "POST", |
| 45 | + url: url, |
| 46 | + body: payload, |
| 47 | + }, &res) |
| 48 | + if err != nil { |
| 49 | + return res, err |
| 50 | + } |
| 51 | + res.TeamID = c.teamID(request.TeamID) |
| 52 | + res.ProjectID = request.ProjectID |
| 53 | + return res, nil |
| 54 | +} |
| 55 | + |
| 56 | +type GetCustomEnvironmentRequest struct { |
| 57 | + TeamID string `json:"-"` |
| 58 | + ProjectID string `json:"-"` |
| 59 | + Slug string `json:"-"` |
| 60 | +} |
| 61 | + |
| 62 | +func (c *Client) GetCustomEnvironment(ctx context.Context, request GetCustomEnvironmentRequest) (res CustomEnvironmentResponse, err error) { |
| 63 | + url := fmt.Sprintf("%s/v1/projects/%s/custom-environments/%s", c.baseURL, request.ProjectID, request.Slug) |
| 64 | + if c.teamID(request.TeamID) != "" { |
| 65 | + url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID)) |
| 66 | + } |
| 67 | + tflog.Info(ctx, "getting custom environment", map[string]interface{}{ |
| 68 | + "url": url, |
| 69 | + }) |
| 70 | + err = c.doRequest(clientRequest{ |
| 71 | + ctx: ctx, |
| 72 | + method: "GET", |
| 73 | + url: url, |
| 74 | + }, &res) |
| 75 | + if err != nil { |
| 76 | + return res, err |
| 77 | + } |
| 78 | + res.TeamID = c.teamID(request.TeamID) |
| 79 | + res.ProjectID = request.ProjectID |
| 80 | + return res, nil |
| 81 | + |
| 82 | +} |
| 83 | + |
| 84 | +type UpdateCustomEnvironmentRequest struct { |
| 85 | + TeamID string `json:"-"` |
| 86 | + ProjectID string `json:"-"` |
| 87 | + OldSlug string `json:"-"` // Needed to get the right URL |
| 88 | + Slug string `json:"slug"` |
| 89 | + Description string `json:"description"` |
| 90 | + BranchMatcher *BranchMatcher `json:"branchMatcher"` |
| 91 | +} |
| 92 | + |
| 93 | +func (c *Client) UpdateCustomEnvironment(ctx context.Context, request UpdateCustomEnvironmentRequest) (res CustomEnvironmentResponse, err error) { |
| 94 | + url := fmt.Sprintf("%s/v1/projects/%s/custom-environments/%s", c.baseURL, request.ProjectID, request.OldSlug) |
| 95 | + if c.teamID(request.TeamID) != "" { |
| 96 | + url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID)) |
| 97 | + } |
| 98 | + payload := string(mustMarshal(request)) |
| 99 | + tflog.Info(ctx, "updating custom environment", map[string]interface{}{ |
| 100 | + "url": url, |
| 101 | + "payload": payload, |
| 102 | + }) |
| 103 | + err = c.doRequest(clientRequest{ |
| 104 | + ctx: ctx, |
| 105 | + method: "PATCH", |
| 106 | + url: url, |
| 107 | + body: payload, |
| 108 | + }, &res) |
| 109 | + if err != nil { |
| 110 | + return res, err |
| 111 | + } |
| 112 | + res.TeamID = c.teamID(request.TeamID) |
| 113 | + res.ProjectID = request.ProjectID |
| 114 | + return res, nil |
| 115 | +} |
| 116 | + |
| 117 | +type DeleteCustomEnvironmentRequest struct { |
| 118 | + TeamID string `json:"-"` |
| 119 | + ProjectID string `json:"-"` |
| 120 | + Slug string `json:"-"` |
| 121 | +} |
| 122 | + |
| 123 | +func (c *Client) DeleteCustomEnvironment(ctx context.Context, request DeleteCustomEnvironmentRequest) error { |
| 124 | + url := fmt.Sprintf("%s/v1/projects/%s/custom-environments/%s", c.baseURL, request.ProjectID, request.Slug) |
| 125 | + if c.teamID(request.TeamID) != "" { |
| 126 | + url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID)) |
| 127 | + } |
| 128 | + tflog.Info(ctx, "deleting custom environment", map[string]interface{}{ |
| 129 | + "url": url, |
| 130 | + }) |
| 131 | + err := c.doRequest(clientRequest{ |
| 132 | + ctx: ctx, |
| 133 | + method: "DELETE", |
| 134 | + url: url, |
| 135 | + body: "{ \"deleteUnassignedEnvironmentVariables\": true }", |
| 136 | + }, nil) |
| 137 | + return err |
| 138 | +} |
0 commit comments