Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge tfvars struct into vars struct #75

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions lib/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,24 +229,16 @@ type AllTeamWorkspaceData struct {
Data []TeamWorkspaceData `json:"data"`
}

// TFVar matches the attributes of a terraform environment/workspace's variable
type TFVar struct {
Key string `json:"key"`
Value string `json:"value"`
Hcl bool `json:"hcl"`
Sensitive bool `json:"sensitive"`
}

type WorkspaceUpdateParams struct {
Organization string
WorkspaceFilter string
Attribute string
Value string
}

// ConvertHCLVariable changes a TFVar struct in place by escaping
// ConvertHCLVariable changes a Var struct in place by escaping
// the double quotes and line endings in the Value attribute
func ConvertHCLVariable(tfVar *TFVar) {
func ConvertHCLVariable(tfVar *Var) {
if !tfVar.Hcl {
return
}
Expand All @@ -257,7 +249,7 @@ func ConvertHCLVariable(tfVar *TFVar) {

// GetCreateVariablePayload returns the json needed to make a Post to the
// Terraform vars api
func GetCreateVariablePayload(organization, workspaceName string, tfVar TFVar) string {
func GetCreateVariablePayload(organization, workspaceName string, tfVar Var) string {
return fmt.Sprintf(`
{
"data": {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was debating whether or not to update category here to be what was in tfVar, but left it as is for now. I can change it as needed. Also debated making the whole variable payload and using json.Marshal()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps an important deciding factor is to make any changes now that would be breaking compatibility. Since this function is exported, any change to the signature or its functionality would be breaking. It might actually make sense to un-export this function now. I'm not sure if it's used anywhere outside this library, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points, I can check.

Copy link
Contributor Author

@jason-jackson jason-jackson May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I might as well fix the deprecated filter.

Copy link
Contributor Author

@jason-jackson jason-jackson May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually. Looking at it further, why not use https://github.com/hashicorp/go-tfe?

[EDIT: I see it has a different license, so yeah, we'd need to factor that in]
[EDIT2: Further reading seems to state that we can leave it as this license (Apache2)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tfc-ops predates the existence of go-tfe (perhaps only by days) so that's the primary reason why we haven't used go-tfe. The other would be because go-tfe appears to be a thin wrapper around the API, whereas tfc-ops aims to be more opinionated and feature-rich. I'm not sure whether your question was about moving to eliminate tfc-ops as a library, or whether it was to start using go-tfe within this library. I'm open to either one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In looking to understand what you meant by "deprecated filter" I noticed that the variables API itself is marked as deprecated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking to use the go-tfe code in this library. It might solve some of the deprecation issues.

Expand All @@ -284,7 +276,7 @@ func GetCreateVariablePayload(organization, workspaceName string, tfVar TFVar) s

// GetUpdateVariablePayload returns the json needed to make a Post to the
// Terraform vars api
func GetUpdateVariablePayload(organization, workspaceName, variableID string, tfVar TFVar) string {
func GetUpdateVariablePayload(organization, workspaceName, variableID string, tfVar Var) string {
return fmt.Sprintf(`
{
"data": {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was debating whether or not to update category here to be what was in tfVar, but left it as is for now. I can change it as needed. Also debated making the whole variable payload and using json.Marshal()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same as above)

Expand Down Expand Up @@ -566,7 +558,7 @@ func AssignTeamAccess(workspaceID string, allTeamData AllTeamWorkspaceData) {

// CreateVariable makes a Terraform vars API POST to create a variable
// for a given organization and workspace
func CreateVariable(organization, workspaceName string, tfVar TFVar) {
func CreateVariable(organization, workspaceName string, tfVar Var) {
url := baseURL + "/vars"

ConvertHCLVariable(&tfVar)
Expand All @@ -582,7 +574,7 @@ func CreateVariable(organization, workspaceName string, tfVar TFVar) {

// CreateAllVariables makes several Terraform vars API POSTs to create
// variables for a given organization and workspace
func CreateAllVariables(organization, workspaceName string, tfVars []TFVar) {
func CreateAllVariables(organization, workspaceName string, tfVars []Var) {
for _, nextVar := range tfVars {
CreateVariable(organization, workspaceName, nextVar)
}
Expand Down Expand Up @@ -611,7 +603,7 @@ func GetCreateWorkspacePayload(oc OpsConfig, vcsTokenID string) string {

// UpdateVariable makes a Terraform vars API call to update a variable
// for a given organization and workspace
func UpdateVariable(organization, workspaceName, variableID string, tfVar TFVar) {
func UpdateVariable(organization, workspaceName, variableID string, tfVar Var) {
url := fmt.Sprintf(baseURL+"/vars/%s", variableID)

ConvertHCLVariable(&tfVar)
Expand Down Expand Up @@ -789,18 +781,18 @@ func CloneWorkspace(cfg CloneConfig) ([]string, error) {
sensitiveValue := "TF_ENTERPRISE_SENSITIVE_VAR"
defaultValue := "REPLACE_THIS_VALUE"

tfVars := []TFVar{}
var tfVar TFVar
tfVars := []Var{}
var tfVar Var

for _, nextVar := range variables {
if cfg.CopyVariables {
tfVar = TFVar{
tfVar = Var{
Key: nextVar.Key,
Value: nextVar.Value,
Hcl: nextVar.Hcl,
}
} else {
tfVar = TFVar{
tfVar = Var{
Key: nextVar.Key,
Value: defaultValue,
Hcl: nextVar.Hcl,
Expand All @@ -818,7 +810,8 @@ func CloneWorkspace(cfg CloneConfig) ([]string, error) {

if cfg.DifferentDestinationAccount {
config.token = cfg.AtlasTokenDestination
if _, err := CreateWorkspace(oc, cfg.NewVCSTokenID); err != nil {
_, err := CreateWorkspace(oc, cfg.NewVCSTokenID)
if err != nil {
briskt marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
}
CreateAllVariables(oc.NewOrg, oc.NewName, tfVars)
Expand Down Expand Up @@ -879,7 +872,7 @@ func AddOrUpdateVariable(cfg UpdateConfig) (string, error) {
continue
}
// Found a match
tfVar := TFVar{Key: nextVar.Key, Value: cfg.NewValue, Hcl: false, Sensitive: cfg.SensitiveVariable}
tfVar := Var{Key: nextVar.Key, Value: cfg.NewValue, Hcl: false, Sensitive: cfg.SensitiveVariable}
if !config.readOnly {
UpdateVariable(cfg.Organization, cfg.Workspace, nextVar.ID, tfVar)
}
Expand All @@ -897,7 +890,7 @@ func AddOrUpdateVariable(cfg UpdateConfig) (string, error) {
return "", errors.New("addKeyIfNotFound was set to true but a variable already exists with key " + nextVar.Key)
}

tfVar := TFVar{Key: nextVar.Key, Value: cfg.NewValue, Hcl: false, Sensitive: cfg.SensitiveVariable}
tfVar := Var{Key: nextVar.Key, Value: cfg.NewValue, Hcl: false, Sensitive: cfg.SensitiveVariable}

if !config.readOnly {
UpdateVariable(cfg.Organization, cfg.Workspace, nextVar.ID, tfVar)
Expand All @@ -907,7 +900,7 @@ func AddOrUpdateVariable(cfg UpdateConfig) (string, error) {

// At this point, we haven't found a match
if cfg.AddKeyIfNotFound {
tfVar := TFVar{Key: cfg.SearchString, Value: cfg.NewValue, Hcl: false, Sensitive: cfg.SensitiveVariable}
tfVar := Var{Key: cfg.SearchString, Value: cfg.NewValue, Hcl: false, Sensitive: cfg.SensitiveVariable}

if !config.readOnly {
CreateVariable(cfg.Organization, cfg.Workspace, tfVar)
Expand Down
Loading