Skip to content

Commit

Permalink
Merge pull request #38 from servian/release_api
Browse files Browse the repository at this point in the history
Use the Release API instead of checkpoint.
  • Loading branch information
tristanmorgan authored Dec 7, 2022
2 parents 3dfeb0e + b9c7558 commit 093ed65
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 109 deletions.
83 changes: 0 additions & 83 deletions checkpoint.go

This file was deleted.

2 changes: 1 addition & 1 deletion example/terraform.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
packer {
required_plugins {
terraform = {
version = ">= 0.0.7"
version = ">= 0.0.8"
source = "github.com/servian/terraform"
}
}
Expand Down
50 changes: 25 additions & 25 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
module github.com/servian/packer-plugin-terraform

go 1.18
go 1.19

require (
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/hcl/v2 v2.13.0
github.com/hashicorp/packer-plugin-sdk v0.3.0
github.com/hashicorp/hcl/v2 v2.14.1
github.com/hashicorp/packer-plugin-sdk v0.3.2
github.com/zclconf/go-cty v1.10.0
)

require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/armon/go-metrics v0.4.0 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/aws/aws-sdk-go v1.44.43 // indirect
github.com/aws/aws-sdk-go v1.44.136 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/consul/api v1.13.0 // indirect
github.com/hashicorp/consul/api v1.15.3 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-getter/v2 v2.1.0 // indirect
github.com/hashicorp/go-hclog v1.2.1 // indirect
github.com/hashicorp/go-hclog v1.3.1 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.4.4 // indirect
github.com/hashicorp/go-plugin v1.4.6 // indirect
github.com/hashicorp/go-retryablehttp v0.7.1 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-secure-stdlib/mlock v0.1.2 // indirect
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6 // indirect
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.7 // indirect
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.5.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/serf v0.9.8 // indirect
github.com/hashicorp/vault/api v1.7.2 // indirect
github.com/hashicorp/vault/sdk v0.5.2 // indirect
github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87 // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/hashicorp/vault/api v1.8.2 // indirect
github.com/hashicorp/vault/sdk v0.6.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
Expand All @@ -59,14 +59,14 @@ require (
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
google.golang.org/genproto v0.0.0-20220627200112-0a929928cb33 // indirect
google.golang.org/grpc v1.47.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/crypto v0.2.0 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/time v0.2.0 // indirect
google.golang.org/genproto v0.0.0-20221111202108-142d8a6fa32e // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
)
82 changes: 82 additions & 0 deletions release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"time"

"github.com/hashicorp/go-cleanhttp"
)

// https://api.releases.hashicorp.com/v1/releases/terraform/latest

// CheckResponse is the response for a check request.
type CheckResponse struct {
Builds []*Build `json:"builds"`
LicenseClass string `json:"license_class"`
Name string `json:"name"`
TimestampCreated string `json:"timestamp_created"`
TimestampUpdated string `json:"timestamp_updated"`
UrlChangelog string `json:"url_changelog"`
UrlDockerRegistryDockerhub string `json:"url_docker_registry_dockerhub"`
UrlDockerRegistryEcr string `json:"url_docker_registry_ecr"`
UrlLicense string `json:"url_license"`
UrlProjectWebsite string `json:"url_project_website"`
UrlShasums string `json:"url_shasums"`
UrlSourceRepository string `json:"url_source_repository"`
Version string `json:"version"`
}

// Build is the different OS Arch builds.
type Build struct {
Arch string
OS string
Url string
}

// FetchLatestTerraform grabs the latest verions of Terraform from Hashicorp
func FetchLatestTerraform() (string, error) {
var u url.URL
v := u.Query()

u.Scheme = "https"
u.Host = "api.releases.hashicorp.com"
u.Path = "/v1/releases/terraform/latest"
u.RawQuery = v.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return "", err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "Servian/Packer-Plugin-Terraform")

client := cleanhttp.DefaultClient()

// We use a short timeout since checking for new versions is not critical
// enough to block on if the release api is broken/slow.
client.Timeout = time.Duration(3000) * time.Millisecond

log.Println(fmt.Sprintf("About to fetch from URL: %s", u.String()))
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return "", fmt.Errorf("Unknown status: %d", resp.StatusCode)
}

var r io.Reader = resp.Body

var result CheckResponse
if err := json.NewDecoder(r).Decode(&result); err != nil {
return "", err
}
log.Println(fmt.Sprintf("Got version response: %s", result.Version))
return result.Version, nil
}

0 comments on commit 093ed65

Please sign in to comment.