From 2148d6bf54f3de417070d76b7baf558f34e3b3f8 Mon Sep 17 00:00:00 2001 From: Brad Sickles Date: Tue, 12 Nov 2024 18:22:34 -0500 Subject: [PATCH] Added endpoints to retrieve iac config/overrides files (#101) --- client.go | 3 +++ workspace_config_files.go | 51 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 workspace_config_files.go diff --git a/client.go b/client.go index 4f8a901..77611ad 100644 --- a/client.go +++ b/client.go @@ -115,6 +115,9 @@ func (c *Client) WorkspaceModule() WorkspaceModule { func (c *Client) WorkspaceConfigs() WorkspaceConfigs { return WorkspaceConfigs{Client: c} } +func (c *Client) WorkspaceConfigFiles() WorkspaceConfigFiles { + return WorkspaceConfigFiles{Client: c} +} func (c *Client) WorkspaceOutputs() WorkspaceOutputs { return WorkspaceOutputs{Client: c} } diff --git a/workspace_config_files.go b/workspace_config_files.go new file mode 100644 index 0000000..1a29c33 --- /dev/null +++ b/workspace_config_files.go @@ -0,0 +1,51 @@ +package api + +import ( + "context" + "fmt" + "gopkg.in/nullstone-io/go-api-client.v0/response" + "io" + "net/http" +) + +type WorkspaceConfigFiles struct { + Client *Client +} + +func (w WorkspaceConfigFiles) configPath(stackId, blockId, envId int64) string { + return fmt.Sprintf("orgs/%s/stacks/%d/blocks/%d/envs/%d/config", w.Client.Config.OrgName, stackId, blockId, envId) +} + +func (w WorkspaceConfigFiles) overridesPath(stackId, blockId, envId int64) string { + return fmt.Sprintf("orgs/%s/stacks/%d/blocks/%d/envs/%d/config_variables", w.Client.Config.OrgName, stackId, blockId, envId) +} + +// GetConfigFile - GET /orgs/:orgName/stacks/:stackId/blocks/:blockId/envs/:envId/config +func (w WorkspaceConfigFiles) GetConfigFile(ctx context.Context, stackId, blockId, envId int64, file io.Writer) error { + res, err := w.Client.Do(ctx, http.MethodGet, w.configPath(stackId, blockId, envId), nil, nil, nil) + if err != nil { + return err + } + + if err := response.ReadFile(res, file); response.IsNotFoundError(err) { + return nil + } else if err != nil { + return err + } + return nil +} + +// GetOverridesFile - GET /orgs/:orgName/stacks/:stackId/blocks/:blockId/envs/:envId/config_variables +func (w WorkspaceConfigFiles) GetOverridesFile(ctx context.Context, stackId, blockId, envId int64, file io.Writer) error { + res, err := w.Client.Do(ctx, http.MethodGet, w.overridesPath(stackId, blockId, envId), nil, nil, nil) + if err != nil { + return err + } + + if err := response.ReadFile(res, file); response.IsNotFoundError(err) { + return nil + } else if err != nil { + return err + } + return nil +}