Skip to content

Commit

Permalink
add a custom UserAgent to packngo API calls with the docker-machine-d…
Browse files Browse the repository at this point in the history
…river-packet release tag
  • Loading branch information
displague committed Aug 12, 2020
1 parent fc3202a commit 91e4250
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ const (
consumerToken = "24e70949af5ecd17fe8e867b335fc88e7de8bd4ad617c0403d8769a376ddea72"
)

var _ drivers.Driver = &Driver{}
var (
// version is set by goreleaser at build time
version = "devel"

// build time check that the Driver type implements the Driver interface
_ drivers.Driver = &Driver{}
)

type Driver struct {
*drivers.BaseDriver
Expand All @@ -40,6 +46,7 @@ type Driver struct {
CaCertPath string
SSHKeyID string
UserDataFile string
UserAgentPrefix string
SpotInstance bool
SpotPriceMax float64
TerminationTime *packngo.Timestamp
Expand Down Expand Up @@ -117,6 +124,11 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
Usage: "The Packet Instance Termination Time",
EnvVar: "PACKET_TERMINATION_TIME",
},
mcnflag.StringFlag{
EnvVar: "PACKET_UA_PREFIX",
Name: "packet-ua-prefix",
Usage: fmt.Sprintf("Prefix the User-Agent in Packet API calls with some 'product/version'"),
},
}
}

Expand All @@ -137,6 +149,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.OperatingSystem = flags.String("packet-os")
d.Facility = flags.String("packet-facility-code")
d.BillingCycle = flags.String("packet-billing-cycle")
d.UserAgentPrefix = flags.String("packet-ua-prefix")
d.UserDataFile = flags.String("packet-userdata")

d.Plan = flags.String("packet-plan")
Expand Down Expand Up @@ -424,7 +437,15 @@ func (d *Driver) GetDockerConfigDir() string {
}

func (d *Driver) getClient() *packngo.Client {
return packngo.NewClientWithAuth(consumerToken, d.ApiKey, nil)
client := packngo.NewClientWithAuth(consumerToken, d.ApiKey, nil)
userAgent := fmt.Sprintf("docker-machine-driver-%s/%s %s", d.DriverName(), version, client.UserAgent)

if len(d.UserAgentPrefix) > 0 {
userAgent = fmt.Sprintf("%s %s", d.UserAgentPrefix, userAgent)
}

client.UserAgent = userAgent
return client
}

func (d *Driver) getOsFlavors() ([]string, error) {
Expand Down

0 comments on commit 91e4250

Please sign in to comment.