-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (139 loc) · 4.89 KB
/
main.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"fmt"
"log"
"os"
packer "github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2021-04-30/client/packer_service"
cloud "github.com/hashicorp/hcp-sdk-go/clients/cloud-shared/v1/models"
"github.com/go-openapi/runtime/client"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2021-04-30/models"
"github.com/hashicorp/hcp-sdk-go/httpclient"
)
// Parameters contains all the posisble inputs needed to make API requests.
type Parameters struct {
// ID of the HCP Organization
OrganizationID string
// ID of the HCP Project
ProjectID string
// Human-readable Bucket name
BucketSlug string
// Human-readable Channel name
ChannelSlug string
}
func main() {
bucketSlug := os.Getenv("INPUT_BUCKET")
channelSlug := os.Getenv("INPUT_CHANNEL")
orgID := os.Getenv("HCP_ORGANIZATION_ID")
projID := os.Getenv("HCP_PROJECT_ID")
// Set params
params := Parameters{
OrganizationID: orgID,
ProjectID: projID,
BucketSlug: bucketSlug,
ChannelSlug: channelSlug,
}
// Initialize SDK client
client, err := httpclient.New(httpclient.Config{})
if err != nil {
log.Fatal(err)
}
// Get latest Iteration in Bucket
latestIterationID, err := getLatestIteration(client, ¶ms)
if err != nil {
log.Fatal(err)
}
if channelExists(client, ¶ms) {
// Update Channel to latest Iteration
err = updateChannel(client, ¶ms, latestIterationID)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Channel \"%s\" updated successfully!", channelSlug)
} else {
// Create Channel and set to latest Iteration
createChannel(client, ¶ms, latestIterationID)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Channel \"%s\" created successfully!", channelSlug)
}
}
// getLatestIteration takes returns the ID of the latest Iteration in the Bucket
func getLatestIteration(client *client.Runtime, params *Parameters) (string, error) {
// Initialize packer client
packerClient := packer.New(client, nil)
// Initialize and set request params
reqParams := packer.NewPackerServiceGetBucketParams()
reqParams.LocationOrganizationID = params.OrganizationID
reqParams.LocationProjectID = params.ProjectID
reqParams.BucketSlug = params.BucketSlug
// Send request
resp, err := packerClient.PackerServiceGetBucket(reqParams, nil)
if err != nil {
return "", err
}
return resp.Payload.Bucket.LatestIteration.ID, nil
}
// channelExists returns true if the Channel exists
func channelExists(client *client.Runtime, params *Parameters) bool {
// Initialize packer client
packerClient := packer.New(client, nil)
// Initialize and set request params
reqParams := packer.NewPackerServiceGetChannelParams()
reqParams.LocationOrganizationID = params.OrganizationID
reqParams.LocationProjectID = params.ProjectID
reqParams.BucketSlug = params.BucketSlug
reqParams.Slug = params.ChannelSlug
// Send request
_, err := packerClient.PackerServiceGetChannel(reqParams, nil)
return err == nil
}
// createChannel creates the specified Channel
func createChannel(client *client.Runtime, params *Parameters, iteration string) error {
// Initialize packer client
packerClient := packer.New(client, nil)
// Initialize and set Location object
var location cloud.HashicorpCloudLocationLocation
location.OrganizationID = params.OrganizationID
location.ProjectID = params.ProjectID
// Initialize and set Body object
var body models.HashicorpCloudPackerCreateChannelRequest
body.Location = &location
body.BucketSlug = params.BucketSlug
body.Slug = params.ChannelSlug
body.IterationID = iteration
// Initialize and set request params
reqParams := packer.NewPackerServiceCreateChannelParams()
reqParams.LocationOrganizationID = params.OrganizationID
reqParams.LocationProjectID = params.ProjectID
reqParams.BucketSlug = params.BucketSlug
reqParams.SetBody(&body)
// Send request
_, err := packerClient.PackerServiceCreateChannel(reqParams, nil)
return err
}
// updateChannel points the Channel to the latest Iteration available
func updateChannel(client *client.Runtime, params *Parameters, iteration string) error {
// Initialize packer client
packerClient := packer.New(client, nil)
// Initialize and set Location object
var location cloud.HashicorpCloudLocationLocation
location.OrganizationID = params.OrganizationID
location.ProjectID = params.ProjectID
// Initialize and set Body object
var body models.HashicorpCloudPackerUpdateChannelRequest
body.Location = &location
body.BucketSlug = params.BucketSlug
body.Slug = params.ChannelSlug
body.IterationID = iteration
// Initialize and set request params
reqParams := packer.NewPackerServiceUpdateChannelParams()
reqParams.LocationOrganizationID = params.OrganizationID
reqParams.LocationProjectID = params.ProjectID
reqParams.BucketSlug = params.BucketSlug
reqParams.Slug = params.ChannelSlug
reqParams.SetBody(&body)
// Send request
_, err := packerClient.PackerServiceUpdateChannel(reqParams, nil)
return err
}