Commit 4166c637 authored by Vasiliy Tolstov's avatar Vasiliy Tolstov

add ability to set api url in template

Signed-off-by: default avatarVasiliy Tolstov <v.tolstov@selfip.ru>
parent 008eb58c
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/mitchellh/mapstructure"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
...@@ -16,9 +15,9 @@ import ( ...@@ -16,9 +15,9 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
)
const DIGITALOCEAN_API_URL = "https://api.digitalocean.com" "github.com/mitchellh/mapstructure"
)
type Image struct { type Image struct {
Id uint Id uint
...@@ -55,23 +54,23 @@ type DigitalOceanClient struct { ...@@ -55,23 +54,23 @@ type DigitalOceanClient struct {
// The http client for communicating // The http client for communicating
client *http.Client client *http.Client
// The base URL of the API
BaseURL string
// Credentials // Credentials
ClientID string ClientID string
APIKey string APIKey string
// The base URL of the API
APIURL string
} }
// Creates a new client for communicating with DO // Creates a new client for communicating with DO
func (d DigitalOceanClient) New(client string, key string) *DigitalOceanClient { func (d DigitalOceanClient) New(client string, key string, url string) *DigitalOceanClient {
c := &DigitalOceanClient{ c := &DigitalOceanClient{
client: &http.Client{ client: &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
}, },
}, },
BaseURL: DIGITALOCEAN_API_URL, APIURL: url,
ClientID: client, ClientID: client,
APIKey: key, APIKey: key,
} }
...@@ -229,7 +228,7 @@ func NewRequest(d DigitalOceanClient, path string, params url.Values) (map[strin ...@@ -229,7 +228,7 @@ func NewRequest(d DigitalOceanClient, path string, params url.Values) (map[strin
params.Set("client_id", d.ClientID) params.Set("client_id", d.ClientID)
params.Set("api_key", d.APIKey) params.Set("api_key", d.APIKey)
url := fmt.Sprintf("%s/%s?%s", DIGITALOCEAN_API_URL, path, params.Encode()) url := fmt.Sprintf("%s/%s?%s", d.APIURL, path, params.Encode())
// Do some basic scrubbing so sensitive information doesn't appear in logs // Do some basic scrubbing so sensitive information doesn't appear in logs
scrubbedUrl := strings.Replace(url, d.ClientID, "CLIENT_ID", -1) scrubbedUrl := strings.Replace(url, d.ClientID, "CLIENT_ID", -1)
......
...@@ -6,13 +6,14 @@ package digitalocean ...@@ -6,13 +6,14 @@ package digitalocean
import ( import (
"errors" "errors"
"fmt" "fmt"
"log"
"os"
"time"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/common" "github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/common/uuid" "github.com/mitchellh/packer/common/uuid"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log"
"os"
"time"
) )
// see https://api.digitalocean.com/images/?client_id=[client_id]&api_key=[api_key] // see https://api.digitalocean.com/images/?client_id=[client_id]&api_key=[api_key]
...@@ -38,6 +39,7 @@ type config struct { ...@@ -38,6 +39,7 @@ type config struct {
ClientID string `mapstructure:"client_id"` ClientID string `mapstructure:"client_id"`
APIKey string `mapstructure:"api_key"` APIKey string `mapstructure:"api_key"`
APIURL string `mapstructure:"api_url"`
RegionID uint `mapstructure:"region_id"` RegionID uint `mapstructure:"region_id"`
SizeID uint `mapstructure:"size_id"` SizeID uint `mapstructure:"size_id"`
ImageID uint `mapstructure:"image_id"` ImageID uint `mapstructure:"image_id"`
...@@ -94,6 +96,11 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { ...@@ -94,6 +96,11 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
b.config.ClientID = os.Getenv("DIGITALOCEAN_CLIENT_ID") b.config.ClientID = os.Getenv("DIGITALOCEAN_CLIENT_ID")
} }
if b.config.APIURL == "" {
// Default to environment variable for api_url, if it exists
b.config.APIURL = os.Getenv("DIGITALOCEAN_API_URL")
}
if b.config.Region == "" { if b.config.Region == "" {
if b.config.RegionID != 0 { if b.config.RegionID != 0 {
b.config.Region = fmt.Sprintf("%v", b.config.RegionID) b.config.Region = fmt.Sprintf("%v", b.config.RegionID)
...@@ -153,6 +160,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { ...@@ -153,6 +160,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
templates := map[string]*string{ templates := map[string]*string{
"client_id": &b.config.ClientID, "client_id": &b.config.ClientID,
"api_key": &b.config.APIKey, "api_key": &b.config.APIKey,
"api_url": &b.config.APIURL,
"snapshot_name": &b.config.SnapshotName, "snapshot_name": &b.config.SnapshotName,
"droplet_name": &b.config.DropletName, "droplet_name": &b.config.DropletName,
"ssh_username": &b.config.SSHUsername, "ssh_username": &b.config.SSHUsername,
...@@ -175,6 +183,10 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { ...@@ -175,6 +183,10 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
errs, errors.New("a client_id must be specified")) errs, errors.New("a client_id must be specified"))
} }
if b.config.APIURL == "" {
b.config.APIURL = "https://api.digitalocean.com"
}
if b.config.APIKey == "" { if b.config.APIKey == "" {
errs = packer.MultiErrorAppend( errs = packer.MultiErrorAppend(
errs, errors.New("an api_key must be specified")) errs, errors.New("an api_key must be specified"))
...@@ -204,7 +216,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { ...@@ -204,7 +216,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
// Initialize the DO API client // Initialize the DO API client
client := DigitalOceanClient{}.New(b.config.ClientID, b.config.APIKey) client := DigitalOceanClient{}.New(b.config.ClientID, b.config.APIKey, b.config.APIURL)
// Set up the state // Set up the state
state := new(multistep.BasicStateBag) state := new(multistep.BasicStateBag)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment