2018-07-04 17:24:49 +02:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
2018-08-30 22:18:43 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-09-17 16:04:49 +02:00
|
|
|
"strings"
|
2018-09-26 17:27:12 +02:00
|
|
|
"time"
|
2018-07-04 17:24:49 +02:00
|
|
|
|
|
|
|
tfe "github.com/hashicorp/go-tfe"
|
2018-09-26 17:27:12 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2018-07-04 17:24:49 +02:00
|
|
|
)
|
|
|
|
|
2018-08-30 22:18:43 +02:00
|
|
|
type mockClient struct {
|
2018-09-26 17:27:12 +02:00
|
|
|
Applies *mockApplies
|
2018-08-30 22:18:43 +02:00
|
|
|
ConfigurationVersions *mockConfigurationVersions
|
|
|
|
Organizations *mockOrganizations
|
|
|
|
Plans *mockPlans
|
2018-09-26 21:35:26 +02:00
|
|
|
PolicyChecks *mockPolicyChecks
|
2018-08-30 22:18:43 +02:00
|
|
|
Runs *mockRuns
|
|
|
|
StateVersions *mockStateVersions
|
|
|
|
Workspaces *mockWorkspaces
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockClient() *mockClient {
|
|
|
|
c := &mockClient{}
|
2018-09-26 17:27:12 +02:00
|
|
|
c.Applies = newMockApplies(c)
|
2018-08-30 22:18:43 +02:00
|
|
|
c.ConfigurationVersions = newMockConfigurationVersions(c)
|
|
|
|
c.Organizations = newMockOrganizations(c)
|
|
|
|
c.Plans = newMockPlans(c)
|
2018-09-26 21:35:26 +02:00
|
|
|
c.PolicyChecks = newMockPolicyChecks(c)
|
2018-08-30 22:18:43 +02:00
|
|
|
c.Runs = newMockRuns(c)
|
|
|
|
c.StateVersions = newMockStateVersions(c)
|
|
|
|
c.Workspaces = newMockWorkspaces(c)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:27:12 +02:00
|
|
|
type mockApplies struct {
|
|
|
|
client *mockClient
|
|
|
|
applies map[string]*tfe.Apply
|
|
|
|
logs map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockApplies(client *mockClient) *mockApplies {
|
|
|
|
return &mockApplies{
|
|
|
|
client: client,
|
|
|
|
applies: make(map[string]*tfe.Apply),
|
|
|
|
logs: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create is a helper function to create a mock apply that uses the configured
|
2018-09-26 21:35:26 +02:00
|
|
|
// working directory to find the logfile.
|
2018-09-26 17:27:12 +02:00
|
|
|
func (m *mockApplies) create(cvID, workspaceID string) (*tfe.Apply, error) {
|
|
|
|
c, ok := m.client.ConfigurationVersions.configVersions[cvID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
if c.Speculative {
|
|
|
|
// Speculative means its plan-only so we don't create a Apply.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
id := generateID("apply-")
|
|
|
|
url := fmt.Sprintf("https://app.terraform.io/_archivist/%s", id)
|
|
|
|
|
|
|
|
a := &tfe.Apply{
|
|
|
|
ID: id,
|
|
|
|
LogReadURL: url,
|
|
|
|
Status: tfe.ApplyPending,
|
|
|
|
}
|
|
|
|
|
|
|
|
w, ok := m.client.Workspaces.workspaceIDs[workspaceID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
|
2018-10-08 12:31:21 +02:00
|
|
|
if w.AutoApply {
|
|
|
|
a.Status = tfe.ApplyRunning
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:27:12 +02:00
|
|
|
m.logs[url] = filepath.Join(
|
|
|
|
m.client.ConfigurationVersions.uploadPaths[cvID],
|
|
|
|
w.WorkingDirectory,
|
|
|
|
"apply.log",
|
|
|
|
)
|
|
|
|
m.applies[a.ID] = a
|
|
|
|
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockApplies) Read(ctx context.Context, applyID string) (*tfe.Apply, error) {
|
|
|
|
a, ok := m.applies[applyID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
// Together with the mockLogReader this allows testing queued runs.
|
|
|
|
if a.Status == tfe.ApplyRunning {
|
|
|
|
a.Status = tfe.ApplyFinished
|
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockApplies) Logs(ctx context.Context, applyID string) (io.Reader, error) {
|
|
|
|
a, err := m.Read(ctx, applyID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logfile, ok := m.logs[a.LogReadURL]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(logfile); os.IsNotExist(err) {
|
|
|
|
return bytes.NewBufferString("logfile does not exist"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
logs, err := ioutil.ReadFile(logfile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
done := func() (bool, error) {
|
|
|
|
a, err := m.Read(ctx, applyID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if a.Status != tfe.ApplyFinished {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &mockLogReader{
|
|
|
|
done: done,
|
|
|
|
logs: bytes.NewBuffer(logs),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-07-04 17:24:49 +02:00
|
|
|
type mockConfigurationVersions struct {
|
2018-08-30 22:18:43 +02:00
|
|
|
client *mockClient
|
2018-07-04 17:24:49 +02:00
|
|
|
configVersions map[string]*tfe.ConfigurationVersion
|
2018-08-30 22:18:43 +02:00
|
|
|
uploadPaths map[string]string
|
2018-07-04 17:24:49 +02:00
|
|
|
uploadURLs map[string]*tfe.ConfigurationVersion
|
|
|
|
}
|
|
|
|
|
2018-08-30 22:18:43 +02:00
|
|
|
func newMockConfigurationVersions(client *mockClient) *mockConfigurationVersions {
|
2018-07-04 17:24:49 +02:00
|
|
|
return &mockConfigurationVersions{
|
2018-08-30 22:18:43 +02:00
|
|
|
client: client,
|
2018-07-04 17:24:49 +02:00
|
|
|
configVersions: make(map[string]*tfe.ConfigurationVersion),
|
2018-08-30 22:18:43 +02:00
|
|
|
uploadPaths: make(map[string]string),
|
2018-07-04 17:24:49 +02:00
|
|
|
uploadURLs: make(map[string]*tfe.ConfigurationVersion),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-08 10:40:28 +02:00
|
|
|
func (m *mockConfigurationVersions) List(ctx context.Context, workspaceID string, options tfe.ConfigurationVersionListOptions) (*tfe.ConfigurationVersionList, error) {
|
|
|
|
cvl := &tfe.ConfigurationVersionList{}
|
2018-07-04 17:24:49 +02:00
|
|
|
for _, cv := range m.configVersions {
|
2018-09-08 10:40:28 +02:00
|
|
|
cvl.Items = append(cvl.Items, cv)
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
2018-09-08 10:40:28 +02:00
|
|
|
|
|
|
|
cvl.Pagination = &tfe.Pagination{
|
|
|
|
CurrentPage: 1,
|
|
|
|
NextPage: 1,
|
|
|
|
PreviousPage: 1,
|
|
|
|
TotalPages: 1,
|
|
|
|
TotalCount: len(cvl.Items),
|
|
|
|
}
|
|
|
|
|
|
|
|
return cvl, nil
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockConfigurationVersions) Create(ctx context.Context, workspaceID string, options tfe.ConfigurationVersionCreateOptions) (*tfe.ConfigurationVersion, error) {
|
|
|
|
id := generateID("cv-")
|
|
|
|
url := fmt.Sprintf("https://app.terraform.io/_archivist/%s", id)
|
|
|
|
|
|
|
|
cv := &tfe.ConfigurationVersion{
|
|
|
|
ID: id,
|
|
|
|
Status: tfe.ConfigurationPending,
|
|
|
|
UploadURL: url,
|
|
|
|
}
|
|
|
|
|
|
|
|
m.configVersions[cv.ID] = cv
|
|
|
|
m.uploadURLs[url] = cv
|
|
|
|
|
|
|
|
return cv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockConfigurationVersions) Read(ctx context.Context, cvID string) (*tfe.ConfigurationVersion, error) {
|
|
|
|
cv, ok := m.configVersions[cvID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
return cv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockConfigurationVersions) Upload(ctx context.Context, url, path string) error {
|
|
|
|
cv, ok := m.uploadURLs[url]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("404 not found")
|
|
|
|
}
|
2018-08-30 22:18:43 +02:00
|
|
|
m.uploadPaths[cv.ID] = path
|
2018-07-04 17:24:49 +02:00
|
|
|
cv.Status = tfe.ConfigurationUploaded
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:27:12 +02:00
|
|
|
// mockInput is a mock implementation of terraform.UIInput.
|
|
|
|
type mockInput struct {
|
|
|
|
answers map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockInput) Input(opts *terraform.InputOpts) (string, error) {
|
|
|
|
v, ok := m.answers[opts.Id]
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("unexpected input request in test: %s", opts.Id)
|
|
|
|
}
|
|
|
|
delete(m.answers, opts.Id)
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2018-07-04 17:24:49 +02:00
|
|
|
type mockOrganizations struct {
|
2018-08-30 22:18:43 +02:00
|
|
|
client *mockClient
|
2018-07-04 17:24:49 +02:00
|
|
|
organizations map[string]*tfe.Organization
|
|
|
|
}
|
|
|
|
|
2018-08-30 22:18:43 +02:00
|
|
|
func newMockOrganizations(client *mockClient) *mockOrganizations {
|
2018-07-04 17:24:49 +02:00
|
|
|
return &mockOrganizations{
|
2018-08-30 22:18:43 +02:00
|
|
|
client: client,
|
2018-07-04 17:24:49 +02:00
|
|
|
organizations: make(map[string]*tfe.Organization),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-08 10:40:28 +02:00
|
|
|
func (m *mockOrganizations) List(ctx context.Context, options tfe.OrganizationListOptions) (*tfe.OrganizationList, error) {
|
|
|
|
orgl := &tfe.OrganizationList{}
|
2018-07-04 17:24:49 +02:00
|
|
|
for _, org := range m.organizations {
|
2018-09-08 10:40:28 +02:00
|
|
|
orgl.Items = append(orgl.Items, org)
|
|
|
|
}
|
|
|
|
|
|
|
|
orgl.Pagination = &tfe.Pagination{
|
|
|
|
CurrentPage: 1,
|
|
|
|
NextPage: 1,
|
|
|
|
PreviousPage: 1,
|
|
|
|
TotalPages: 1,
|
|
|
|
TotalCount: len(orgl.Items),
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
2018-09-08 10:40:28 +02:00
|
|
|
|
|
|
|
return orgl, nil
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:27:12 +02:00
|
|
|
// mockLogReader is a mock logreader that enables testing queued runs.
|
|
|
|
type mockLogReader struct {
|
|
|
|
done func() (bool, error)
|
|
|
|
logs *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockLogReader) Read(l []byte) (int, error) {
|
|
|
|
for {
|
|
|
|
if written, err := m.read(l); err != io.ErrNoProgress {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockLogReader) read(l []byte) (int, error) {
|
|
|
|
done, err := m.done()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if !done {
|
|
|
|
return 0, io.ErrNoProgress
|
|
|
|
}
|
|
|
|
return m.logs.Read(l)
|
|
|
|
}
|
|
|
|
|
2018-07-04 17:24:49 +02:00
|
|
|
func (m *mockOrganizations) Create(ctx context.Context, options tfe.OrganizationCreateOptions) (*tfe.Organization, error) {
|
|
|
|
org := &tfe.Organization{Name: *options.Name}
|
|
|
|
m.organizations[org.Name] = org
|
|
|
|
return org, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockOrganizations) Read(ctx context.Context, name string) (*tfe.Organization, error) {
|
|
|
|
org, ok := m.organizations[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
return org, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockOrganizations) Update(ctx context.Context, name string, options tfe.OrganizationUpdateOptions) (*tfe.Organization, error) {
|
|
|
|
org, ok := m.organizations[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
org.Name = *options.Name
|
|
|
|
return org, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockOrganizations) Delete(ctx context.Context, name string) error {
|
|
|
|
delete(m.organizations, name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockPlans struct {
|
2018-08-30 22:18:43 +02:00
|
|
|
client *mockClient
|
|
|
|
logs map[string]string
|
|
|
|
plans map[string]*tfe.Plan
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 22:18:43 +02:00
|
|
|
func newMockPlans(client *mockClient) *mockPlans {
|
2018-07-04 17:24:49 +02:00
|
|
|
return &mockPlans{
|
2018-08-30 22:18:43 +02:00
|
|
|
client: client,
|
|
|
|
logs: make(map[string]string),
|
|
|
|
plans: make(map[string]*tfe.Plan),
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 22:18:43 +02:00
|
|
|
// create is a helper function to create a mock plan that uses the configured
|
2018-09-26 21:35:26 +02:00
|
|
|
// working directory to find the logfile.
|
2018-08-30 22:18:43 +02:00
|
|
|
func (m *mockPlans) create(cvID, workspaceID string) (*tfe.Plan, error) {
|
|
|
|
id := generateID("plan-")
|
|
|
|
url := fmt.Sprintf("https://app.terraform.io/_archivist/%s", id)
|
2018-07-04 17:24:49 +02:00
|
|
|
|
2018-08-30 22:18:43 +02:00
|
|
|
p := &tfe.Plan{
|
|
|
|
ID: id,
|
|
|
|
LogReadURL: url,
|
|
|
|
Status: tfe.PlanPending,
|
|
|
|
}
|
2018-07-04 17:24:49 +02:00
|
|
|
|
2018-08-30 22:18:43 +02:00
|
|
|
w, ok := m.client.Workspaces.workspaceIDs[workspaceID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 22:18:43 +02:00
|
|
|
m.logs[url] = filepath.Join(
|
|
|
|
m.client.ConfigurationVersions.uploadPaths[cvID],
|
|
|
|
w.WorkingDirectory,
|
2018-09-26 17:27:12 +02:00
|
|
|
"plan.log",
|
2018-08-30 22:18:43 +02:00
|
|
|
)
|
|
|
|
m.plans[p.ID] = p
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPlans) Read(ctx context.Context, planID string) (*tfe.Plan, error) {
|
|
|
|
p, ok := m.plans[planID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
2018-09-26 17:27:12 +02:00
|
|
|
// Together with the mockLogReader this allows testing queued runs.
|
|
|
|
if p.Status == tfe.PlanRunning {
|
|
|
|
p.Status = tfe.PlanFinished
|
|
|
|
}
|
2018-07-04 17:24:49 +02:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPlans) Logs(ctx context.Context, planID string) (io.Reader, error) {
|
|
|
|
p, err := m.Read(ctx, planID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logfile, ok := m.logs[p.LogReadURL]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
|
2018-08-30 22:18:43 +02:00
|
|
|
if _, err := os.Stat(logfile); os.IsNotExist(err) {
|
|
|
|
return bytes.NewBufferString("logfile does not exist"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
logs, err := ioutil.ReadFile(logfile)
|
2018-07-04 17:24:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:27:12 +02:00
|
|
|
done := func() (bool, error) {
|
|
|
|
p, err := m.Read(ctx, planID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if p.Status != tfe.PlanFinished {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &mockLogReader{
|
|
|
|
done: done,
|
|
|
|
logs: bytes.NewBuffer(logs),
|
|
|
|
}, nil
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 21:35:26 +02:00
|
|
|
type mockPolicyChecks struct {
|
|
|
|
client *mockClient
|
|
|
|
checks map[string]*tfe.PolicyCheck
|
|
|
|
logs map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockPolicyChecks(client *mockClient) *mockPolicyChecks {
|
|
|
|
return &mockPolicyChecks{
|
|
|
|
client: client,
|
|
|
|
checks: make(map[string]*tfe.PolicyCheck),
|
|
|
|
logs: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create is a helper function to create a mock policy check that uses the
|
|
|
|
// configured working directory to find the logfile.
|
|
|
|
func (m *mockPolicyChecks) create(cvID, workspaceID string) (*tfe.PolicyCheck, error) {
|
|
|
|
id := generateID("pc-")
|
|
|
|
|
|
|
|
pc := &tfe.PolicyCheck{
|
|
|
|
ID: id,
|
|
|
|
Actions: &tfe.PolicyActions{},
|
|
|
|
Permissions: &tfe.PolicyPermissions{},
|
|
|
|
Scope: tfe.PolicyScopeOrganization,
|
|
|
|
Status: tfe.PolicyPending,
|
|
|
|
}
|
|
|
|
|
|
|
|
w, ok := m.client.Workspaces.workspaceIDs[workspaceID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
logfile := filepath.Join(
|
|
|
|
m.client.ConfigurationVersions.uploadPaths[cvID],
|
|
|
|
w.WorkingDirectory,
|
|
|
|
"policy.log",
|
|
|
|
)
|
|
|
|
|
|
|
|
if _, err := os.Stat(logfile); os.IsNotExist(err) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m.logs[pc.ID] = logfile
|
|
|
|
m.checks[pc.ID] = pc
|
|
|
|
|
|
|
|
return pc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPolicyChecks) List(ctx context.Context, runID string, options tfe.PolicyCheckListOptions) (*tfe.PolicyCheckList, error) {
|
|
|
|
_, ok := m.client.Runs.runs[runID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
pcl := &tfe.PolicyCheckList{}
|
|
|
|
for _, pc := range m.checks {
|
|
|
|
pcl.Items = append(pcl.Items, pc)
|
|
|
|
}
|
|
|
|
|
|
|
|
pcl.Pagination = &tfe.Pagination{
|
|
|
|
CurrentPage: 1,
|
|
|
|
NextPage: 1,
|
|
|
|
PreviousPage: 1,
|
|
|
|
TotalPages: 1,
|
|
|
|
TotalCount: len(pcl.Items),
|
|
|
|
}
|
|
|
|
|
|
|
|
return pcl, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPolicyChecks) Read(ctx context.Context, policyCheckID string) (*tfe.PolicyCheck, error) {
|
|
|
|
pc, ok := m.checks[policyCheckID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
logfile, ok := m.logs[pc.ID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(logfile); os.IsNotExist(err) {
|
2018-10-01 12:00:23 +02:00
|
|
|
return nil, fmt.Errorf("logfile does not exist")
|
2018-09-26 21:35:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logs, err := ioutil.ReadFile(logfile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case bytes.Contains(logs, []byte("Sentinel Result: true")):
|
|
|
|
pc.Status = tfe.PolicyPasses
|
|
|
|
case bytes.Contains(logs, []byte("Sentinel Result: false")):
|
|
|
|
switch {
|
|
|
|
case bytes.Contains(logs, []byte("hard-mandatory")):
|
|
|
|
pc.Status = tfe.PolicyHardFailed
|
|
|
|
case bytes.Contains(logs, []byte("soft-mandatory")):
|
|
|
|
pc.Actions.IsOverridable = true
|
|
|
|
pc.Permissions.CanOverride = true
|
|
|
|
pc.Status = tfe.PolicySoftFailed
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// As this is an unexpected state, we say the policy errored.
|
|
|
|
pc.Status = tfe.PolicyErrored
|
|
|
|
}
|
|
|
|
|
2018-10-01 12:00:23 +02:00
|
|
|
return pc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPolicyChecks) Override(ctx context.Context, policyCheckID string) (*tfe.PolicyCheck, error) {
|
|
|
|
pc, ok := m.checks[policyCheckID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
pc.Status = tfe.PolicyOverridden
|
|
|
|
return pc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPolicyChecks) Logs(ctx context.Context, policyCheckID string) (io.Reader, error) {
|
|
|
|
pc, ok := m.checks[policyCheckID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
logfile, ok := m.logs[pc.ID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(logfile); os.IsNotExist(err) {
|
2018-10-04 23:03:40 +02:00
|
|
|
return bytes.NewBufferString("logfile does not exist"), nil
|
2018-10-01 12:00:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logs, err := ioutil.ReadFile(logfile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-04 23:03:40 +02:00
|
|
|
switch {
|
|
|
|
case bytes.Contains(logs, []byte("Sentinel Result: true")):
|
|
|
|
pc.Status = tfe.PolicyPasses
|
|
|
|
case bytes.Contains(logs, []byte("Sentinel Result: false")):
|
|
|
|
switch {
|
|
|
|
case bytes.Contains(logs, []byte("hard-mandatory")):
|
|
|
|
pc.Status = tfe.PolicyHardFailed
|
|
|
|
case bytes.Contains(logs, []byte("soft-mandatory")):
|
|
|
|
pc.Actions.IsOverridable = true
|
|
|
|
pc.Permissions.CanOverride = true
|
|
|
|
pc.Status = tfe.PolicySoftFailed
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// As this is an unexpected state, we say the policy errored.
|
|
|
|
pc.Status = tfe.PolicyErrored
|
|
|
|
}
|
|
|
|
|
2018-09-26 21:35:26 +02:00
|
|
|
return bytes.NewBuffer(logs), nil
|
|
|
|
}
|
|
|
|
|
2018-07-04 17:24:49 +02:00
|
|
|
type mockRuns struct {
|
2018-08-30 22:18:43 +02:00
|
|
|
client *mockClient
|
2018-07-04 17:24:49 +02:00
|
|
|
runs map[string]*tfe.Run
|
|
|
|
workspaces map[string][]*tfe.Run
|
|
|
|
}
|
|
|
|
|
2018-08-30 22:18:43 +02:00
|
|
|
func newMockRuns(client *mockClient) *mockRuns {
|
2018-07-04 17:24:49 +02:00
|
|
|
return &mockRuns{
|
2018-08-30 22:18:43 +02:00
|
|
|
client: client,
|
2018-07-04 17:24:49 +02:00
|
|
|
runs: make(map[string]*tfe.Run),
|
|
|
|
workspaces: make(map[string][]*tfe.Run),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-08 10:40:28 +02:00
|
|
|
func (m *mockRuns) List(ctx context.Context, workspaceID string, options tfe.RunListOptions) (*tfe.RunList, error) {
|
2018-08-30 22:18:43 +02:00
|
|
|
w, ok := m.client.Workspaces.workspaceIDs[workspaceID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
2018-09-08 10:40:28 +02:00
|
|
|
|
|
|
|
rl := &tfe.RunList{}
|
2018-08-30 22:18:43 +02:00
|
|
|
for _, r := range m.workspaces[w.ID] {
|
2018-09-08 10:40:28 +02:00
|
|
|
rl.Items = append(rl.Items, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
rl.Pagination = &tfe.Pagination{
|
|
|
|
CurrentPage: 1,
|
|
|
|
NextPage: 1,
|
|
|
|
PreviousPage: 1,
|
|
|
|
TotalPages: 1,
|
|
|
|
TotalCount: len(rl.Items),
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
2018-09-08 10:40:28 +02:00
|
|
|
|
|
|
|
return rl, nil
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockRuns) Create(ctx context.Context, options tfe.RunCreateOptions) (*tfe.Run, error) {
|
2018-09-26 17:27:12 +02:00
|
|
|
a, err := m.client.Applies.create(options.ConfigurationVersion.ID, options.Workspace.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-08-30 22:18:43 +02:00
|
|
|
p, err := m.client.Plans.create(options.ConfigurationVersion.ID, options.Workspace.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 21:35:26 +02:00
|
|
|
pc, err := m.client.PolicyChecks.create(options.ConfigurationVersion.ID, options.Workspace.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-04 17:24:49 +02:00
|
|
|
r := &tfe.Run{
|
2018-09-26 17:27:12 +02:00
|
|
|
ID: generateID("run-"),
|
2018-10-05 17:39:06 +02:00
|
|
|
Actions: &tfe.RunActions{IsCancelable: true},
|
2018-09-26 17:27:12 +02:00
|
|
|
Apply: a,
|
2018-10-05 17:39:06 +02:00
|
|
|
HasChanges: false,
|
2018-09-26 17:27:12 +02:00
|
|
|
Permissions: &tfe.RunPermissions{},
|
|
|
|
Plan: p,
|
|
|
|
Status: tfe.RunPending,
|
|
|
|
}
|
|
|
|
|
2018-09-26 21:35:26 +02:00
|
|
|
if pc != nil {
|
|
|
|
r.PolicyChecks = []*tfe.PolicyCheck{pc}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:27:12 +02:00
|
|
|
if options.IsDestroy != nil {
|
|
|
|
r.IsDestroy = *options.IsDestroy
|
|
|
|
}
|
|
|
|
|
2018-07-04 17:24:49 +02:00
|
|
|
m.runs[r.ID] = r
|
|
|
|
m.workspaces[options.Workspace.ID] = append(m.workspaces[options.Workspace.ID], r)
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockRuns) Read(ctx context.Context, runID string) (*tfe.Run, error) {
|
|
|
|
r, ok := m.runs[runID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
2018-09-26 17:27:12 +02:00
|
|
|
|
|
|
|
pending := false
|
|
|
|
for _, r := range m.runs {
|
|
|
|
if r.ID != runID && r.Status == tfe.RunPending {
|
|
|
|
pending = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 17:39:06 +02:00
|
|
|
if !pending && r.Status == tfe.RunPending {
|
2018-09-26 17:27:12 +02:00
|
|
|
// Only update the status if there are no other pending runs.
|
|
|
|
r.Status = tfe.RunPlanning
|
|
|
|
r.Plan.Status = tfe.PlanRunning
|
|
|
|
}
|
|
|
|
|
2018-10-05 17:39:06 +02:00
|
|
|
logs, _ := ioutil.ReadFile(m.client.Plans.logs[r.Plan.LogReadURL])
|
|
|
|
if r.Plan.Status == tfe.PlanFinished {
|
|
|
|
if r.IsDestroy || bytes.Contains(logs, []byte("1 to add, 0 to change, 0 to destroy")) {
|
|
|
|
r.Actions.IsCancelable = false
|
|
|
|
r.Actions.IsConfirmable = true
|
|
|
|
r.HasChanges = true
|
|
|
|
r.Permissions.CanApply = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Contains(logs, []byte("null_resource.foo: 1 error")) {
|
|
|
|
r.Actions.IsCancelable = false
|
|
|
|
r.HasChanges = false
|
|
|
|
r.Status = tfe.RunErrored
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 17:24:49 +02:00
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockRuns) Apply(ctx context.Context, runID string, options tfe.RunApplyOptions) error {
|
2018-09-26 17:27:12 +02:00
|
|
|
r, ok := m.runs[runID]
|
|
|
|
if !ok {
|
|
|
|
return tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
if r.Status != tfe.RunPending {
|
|
|
|
// Only update the status if the run is not pending anymore.
|
|
|
|
r.Status = tfe.RunApplying
|
|
|
|
r.Apply.Status = tfe.ApplyRunning
|
|
|
|
}
|
|
|
|
return nil
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockRuns) Cancel(ctx context.Context, runID string, options tfe.RunCancelOptions) error {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockRuns) Discard(ctx context.Context, runID string, options tfe.RunDiscardOptions) error {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockStateVersions struct {
|
2018-08-30 22:18:43 +02:00
|
|
|
client *mockClient
|
2018-07-04 17:24:49 +02:00
|
|
|
states map[string][]byte
|
|
|
|
stateVersions map[string]*tfe.StateVersion
|
|
|
|
workspaces map[string][]string
|
|
|
|
}
|
|
|
|
|
2018-08-30 22:18:43 +02:00
|
|
|
func newMockStateVersions(client *mockClient) *mockStateVersions {
|
2018-07-04 17:24:49 +02:00
|
|
|
return &mockStateVersions{
|
2018-08-30 22:18:43 +02:00
|
|
|
client: client,
|
2018-07-04 17:24:49 +02:00
|
|
|
states: make(map[string][]byte),
|
|
|
|
stateVersions: make(map[string]*tfe.StateVersion),
|
|
|
|
workspaces: make(map[string][]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-08 10:40:28 +02:00
|
|
|
func (m *mockStateVersions) List(ctx context.Context, options tfe.StateVersionListOptions) (*tfe.StateVersionList, error) {
|
|
|
|
svl := &tfe.StateVersionList{}
|
2018-07-04 17:24:49 +02:00
|
|
|
for _, sv := range m.stateVersions {
|
2018-09-08 10:40:28 +02:00
|
|
|
svl.Items = append(svl.Items, sv)
|
|
|
|
}
|
|
|
|
|
|
|
|
svl.Pagination = &tfe.Pagination{
|
|
|
|
CurrentPage: 1,
|
|
|
|
NextPage: 1,
|
|
|
|
PreviousPage: 1,
|
|
|
|
TotalPages: 1,
|
|
|
|
TotalCount: len(svl.Items),
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
2018-09-08 10:40:28 +02:00
|
|
|
|
|
|
|
return svl, nil
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockStateVersions) Create(ctx context.Context, workspaceID string, options tfe.StateVersionCreateOptions) (*tfe.StateVersion, error) {
|
|
|
|
id := generateID("sv-")
|
2018-09-08 11:38:21 +02:00
|
|
|
runID := os.Getenv("TFE_RUN_ID")
|
2018-07-04 17:24:49 +02:00
|
|
|
url := fmt.Sprintf("https://app.terraform.io/_archivist/%s", id)
|
|
|
|
|
2018-09-08 11:38:21 +02:00
|
|
|
if runID != "" && (options.Run == nil || runID != options.Run.ID) {
|
|
|
|
return nil, fmt.Errorf("option.Run.ID does not contain the ID exported by TFE_RUN_ID")
|
|
|
|
}
|
|
|
|
|
2018-07-04 17:24:49 +02:00
|
|
|
sv := &tfe.StateVersion{
|
|
|
|
ID: id,
|
|
|
|
DownloadURL: url,
|
|
|
|
Serial: *options.Serial,
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := base64.StdEncoding.DecodeString(*options.State)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.states[sv.DownloadURL] = state
|
|
|
|
m.stateVersions[sv.ID] = sv
|
|
|
|
m.workspaces[workspaceID] = append(m.workspaces[workspaceID], sv.ID)
|
|
|
|
|
|
|
|
return sv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockStateVersions) Read(ctx context.Context, svID string) (*tfe.StateVersion, error) {
|
|
|
|
sv, ok := m.stateVersions[svID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
return sv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockStateVersions) Current(ctx context.Context, workspaceID string) (*tfe.StateVersion, error) {
|
2018-08-30 22:18:43 +02:00
|
|
|
w, ok := m.client.Workspaces.workspaceIDs[workspaceID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
svs, ok := m.workspaces[w.ID]
|
2018-07-04 17:24:49 +02:00
|
|
|
if !ok || len(svs) == 0 {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
2018-08-30 22:18:43 +02:00
|
|
|
|
2018-07-04 17:24:49 +02:00
|
|
|
sv, ok := m.stateVersions[svs[len(svs)-1]]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
2018-08-30 22:18:43 +02:00
|
|
|
|
2018-07-04 17:24:49 +02:00
|
|
|
return sv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockStateVersions) Download(ctx context.Context, url string) ([]byte, error) {
|
|
|
|
state, ok := m.states[url]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockWorkspaces struct {
|
2018-08-30 22:18:43 +02:00
|
|
|
client *mockClient
|
2018-07-04 17:24:49 +02:00
|
|
|
workspaceIDs map[string]*tfe.Workspace
|
|
|
|
workspaceNames map[string]*tfe.Workspace
|
|
|
|
}
|
|
|
|
|
2018-08-30 22:18:43 +02:00
|
|
|
func newMockWorkspaces(client *mockClient) *mockWorkspaces {
|
2018-07-04 17:24:49 +02:00
|
|
|
return &mockWorkspaces{
|
2018-08-30 22:18:43 +02:00
|
|
|
client: client,
|
2018-07-04 17:24:49 +02:00
|
|
|
workspaceIDs: make(map[string]*tfe.Workspace),
|
|
|
|
workspaceNames: make(map[string]*tfe.Workspace),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-08 10:40:28 +02:00
|
|
|
func (m *mockWorkspaces) List(ctx context.Context, organization string, options tfe.WorkspaceListOptions) (*tfe.WorkspaceList, error) {
|
2018-09-08 11:12:03 +02:00
|
|
|
dummyWorkspaces := 10
|
2018-09-08 10:40:28 +02:00
|
|
|
wl := &tfe.WorkspaceList{}
|
2018-09-08 11:12:03 +02:00
|
|
|
|
2018-09-17 16:04:49 +02:00
|
|
|
// Get the prefix from the search options.
|
|
|
|
prefix := ""
|
|
|
|
if options.Search != nil {
|
|
|
|
prefix = *options.Search
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all the workspaces that match the prefix.
|
|
|
|
var ws []*tfe.Workspace
|
|
|
|
for _, w := range m.workspaceIDs {
|
|
|
|
if strings.HasPrefix(w.Name, prefix) {
|
|
|
|
ws = append(ws, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an empty result if we have no matches.
|
|
|
|
if len(ws) == 0 {
|
|
|
|
wl.Pagination = &tfe.Pagination{
|
|
|
|
CurrentPage: 1,
|
|
|
|
}
|
|
|
|
return wl, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return dummy workspaces for the first page to test pagination.
|
2018-09-08 11:12:03 +02:00
|
|
|
if options.PageNumber <= 1 {
|
|
|
|
for i := 0; i < dummyWorkspaces; i++ {
|
|
|
|
wl.Items = append(wl.Items, &tfe.Workspace{
|
|
|
|
ID: generateID("ws-"),
|
|
|
|
Name: fmt.Sprintf("dummy-workspace-%d", i),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
wl.Pagination = &tfe.Pagination{
|
|
|
|
CurrentPage: 1,
|
|
|
|
NextPage: 2,
|
|
|
|
TotalPages: 2,
|
2018-09-17 16:04:49 +02:00
|
|
|
TotalCount: len(wl.Items) + len(ws),
|
2018-09-08 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return wl, nil
|
|
|
|
}
|
|
|
|
|
2018-09-17 16:04:49 +02:00
|
|
|
// Return the actual workspaces that matched as the second page.
|
|
|
|
wl.Items = ws
|
2018-09-08 10:40:28 +02:00
|
|
|
wl.Pagination = &tfe.Pagination{
|
2018-09-08 11:12:03 +02:00
|
|
|
CurrentPage: 2,
|
2018-09-08 10:40:28 +02:00
|
|
|
PreviousPage: 1,
|
2018-09-08 11:12:03 +02:00
|
|
|
TotalPages: 2,
|
|
|
|
TotalCount: len(wl.Items) + dummyWorkspaces,
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
2018-09-08 10:40:28 +02:00
|
|
|
|
|
|
|
return wl, nil
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockWorkspaces) Create(ctx context.Context, organization string, options tfe.WorkspaceCreateOptions) (*tfe.Workspace, error) {
|
|
|
|
w := &tfe.Workspace{
|
2018-08-30 22:18:43 +02:00
|
|
|
ID: generateID("ws-"),
|
2018-07-04 17:24:49 +02:00
|
|
|
Name: *options.Name,
|
2018-10-04 22:53:56 +02:00
|
|
|
Permissions: &tfe.WorkspacePermissions{
|
|
|
|
CanQueueRun: true,
|
|
|
|
CanUpdate: true,
|
|
|
|
},
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
2018-10-08 12:31:21 +02:00
|
|
|
if options.AutoApply != nil {
|
|
|
|
w.AutoApply = *options.AutoApply
|
|
|
|
}
|
2018-09-26 17:27:12 +02:00
|
|
|
if options.VCSRepo != nil {
|
|
|
|
w.VCSRepo = &tfe.VCSRepo{}
|
|
|
|
}
|
2018-07-04 17:24:49 +02:00
|
|
|
m.workspaceIDs[w.ID] = w
|
|
|
|
m.workspaceNames[w.Name] = w
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockWorkspaces) Read(ctx context.Context, organization, workspace string) (*tfe.Workspace, error) {
|
|
|
|
w, ok := m.workspaceNames[workspace]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockWorkspaces) Update(ctx context.Context, organization, workspace string, options tfe.WorkspaceUpdateOptions) (*tfe.Workspace, error) {
|
|
|
|
w, ok := m.workspaceNames[workspace]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
2018-08-30 22:18:43 +02:00
|
|
|
|
|
|
|
if options.Name != nil {
|
|
|
|
w.Name = *options.Name
|
|
|
|
}
|
|
|
|
if options.TerraformVersion != nil {
|
|
|
|
w.TerraformVersion = *options.TerraformVersion
|
|
|
|
}
|
|
|
|
if options.WorkingDirectory != nil {
|
|
|
|
w.WorkingDirectory = *options.WorkingDirectory
|
|
|
|
}
|
2018-07-04 17:24:49 +02:00
|
|
|
|
|
|
|
delete(m.workspaceNames, workspace)
|
|
|
|
m.workspaceNames[w.Name] = w
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockWorkspaces) Delete(ctx context.Context, organization, workspace string) error {
|
|
|
|
if w, ok := m.workspaceNames[workspace]; ok {
|
|
|
|
delete(m.workspaceIDs, w.ID)
|
|
|
|
}
|
|
|
|
delete(m.workspaceNames, workspace)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockWorkspaces) Lock(ctx context.Context, workspaceID string, options tfe.WorkspaceLockOptions) (*tfe.Workspace, error) {
|
2018-09-10 15:31:18 +02:00
|
|
|
w, ok := m.workspaceIDs[workspaceID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
w.Locked = true
|
|
|
|
return w, nil
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockWorkspaces) Unlock(ctx context.Context, workspaceID string) (*tfe.Workspace, error) {
|
2018-09-10 15:31:18 +02:00
|
|
|
w, ok := m.workspaceIDs[workspaceID]
|
|
|
|
if !ok {
|
|
|
|
return nil, tfe.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
w.Locked = false
|
|
|
|
return w, nil
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockWorkspaces) AssignSSHKey(ctx context.Context, workspaceID string, options tfe.WorkspaceAssignSSHKeyOptions) (*tfe.Workspace, error) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockWorkspaces) UnassignSSHKey(ctx context.Context, workspaceID string) (*tfe.Workspace, error) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
const alphanumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
|
|
|
|
func generateID(s string) string {
|
|
|
|
b := make([]byte, 16)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = alphanumeric[rand.Intn(len(alphanumeric))]
|
|
|
|
}
|
|
|
|
return s + string(b)
|
|
|
|
}
|