File: terraform.go

package info (click to toggle)
golang-github-hashicorp-atlas-go 0.0~git20230125.46e9b3e-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 528 kB
  • sloc: sh: 262; makefile: 17
file content (106 lines) | stat: -rw-r--r-- 2,615 bytes parent folder | download | duplicates (2)
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
package atlas

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"log"
)

// TerraformConfigVersion represents a single uploaded version of a
// Terraform configuration.
type TerraformConfigVersion struct {
	Version   int
	Remotes   []string          `json:"remotes"`
	Metadata  map[string]string `json:"metadata"`
	Variables map[string]string `json:"variables,omitempty"`
	TFVars    []TFVar           `json:"tf_vars"`
}

// TFVar is used to serialize a single Terraform variable sent by the
// manager as a collection of Variables in a Job payload.
type TFVar struct {
	Key   string `json:"key"`
	Value string `json:"value"`
	IsHCL bool   `json:"hcl"`
}

// TerraformConfigLatest returns the latest Terraform configuration version.
func (c *Client) TerraformConfigLatest(user, name string) (*TerraformConfigVersion, error) {
	log.Printf("[INFO] getting terraform configuration %s/%s", user, name)

	endpoint := fmt.Sprintf("/api/v1/terraform/configurations/%s/%s/versions/latest", user, name)
	request, err := c.Request("GET", endpoint, nil)
	if err != nil {
		return nil, err
	}

	response, err := checkResp(c.HTTPClient.Do(request))
	if err == ErrNotFound {
		return nil, nil
	}
	if err != nil {
		return nil, err
	}

	var wrapper tfConfigVersionWrapper
	if err := decodeJSON(response, &wrapper); err != nil {
		return nil, err
	}

	return wrapper.Version, nil
}

// CreateTerraformConfigVersion creatse a new Terraform configuration
// versions and uploads a slug with it.
func (c *Client) CreateTerraformConfigVersion(
	user string, name string,
	version *TerraformConfigVersion,
	data io.Reader, size int64) (int, error) {
	log.Printf("[INFO] creating terraform configuration %s/%s", user, name)

	endpoint := fmt.Sprintf(
		"/api/v1/terraform/configurations/%s/%s/versions", user, name)
	body, err := json.Marshal(&tfConfigVersionWrapper{
		Version: version,
	})
	if err != nil {
		return 0, err
	}

	request, err := c.Request("POST", endpoint, &RequestOptions{
		Body: bytes.NewReader(body),
		Headers: map[string]string{
			"Content-Type": "application/json",
		},
	})
	if err != nil {
		return 0, err
	}

	response, err := checkResp(c.HTTPClient.Do(request))
	if err != nil {
		return 0, err
	}

	var result tfConfigVersionCreate
	if err := decodeJSON(response, &result); err != nil {
		return 0, err
	}

	if err := c.putFile(result.UploadPath, data, size); err != nil {
		return 0, err
	}

	return result.Version, nil
}

type tfConfigVersionCreate struct {
	UploadPath string `json:"upload_path"`
	Version    int
}

type tfConfigVersionWrapper struct {
	Version *TerraformConfigVersion `json:"version"`
}