Skip to content

Commit

Permalink
Merge branch 'util-env'
Browse files Browse the repository at this point in the history
  • Loading branch information
alkar committed Jan 22, 2020
2 parents 146ac9b + cc09fd0 commit 3de9716
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 29 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ providerlint:
-c 0 \
./...

reset-token:
@sh -c 'cd util/megaport_token && go run . --reset'

sweep:
@echo "WARNING: This will destroy infrastructure. Use only in development accounts."
go test $(TEST) -v -sweep=$(SWEEP) $(SWEEPARGS)
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

## Utilities

To grab a token for the megaport api, you can use the helper tool:
To retrieve a new token for the megaport api:
```
$ eval $(make reset-token)
```

Alternatively, you can use the helper tool directly:
```
$ cd util/megaport_token
$ go run .
```

To revoke a token (and get a new one) you can pass the `--reset` flag to the
tool.
To revoke the current token (and get a new one) you can pass the `--reset` flag.
72 changes: 47 additions & 25 deletions util/megaport_token/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main

import (
"bufio"
"fmt"
"log"
"os"
"time"

"github.com/pquerna/otp/totp"
"github.com/utilitywarehouse/terraform-provider-megaport/megaport/api"
)

Expand All @@ -14,40 +15,61 @@ const (
)

func main() {
var (
reset = false
endpoint = api.EndpointStaging
username, password, totpSecret string
)
if len(os.Args) > 2 {
log.Fatalln(usage)
}
if len(os.Args) == 2 {
switch os.Args[1] {
case "--reset":
token := os.Getenv("MEGAPORT_TOKEN")
if token == "" {
log.Fatal("To reset the token, please export MEGAPORT_TOKEN with your current token")
}
c := api.NewClient(api.EndpointStaging)
c.Token = token
if err := c.Logout(); err != nil {
log.Fatal(err)
}
log.Print("Current token has been reset. Please login again to fetch a new one.")
reset = true
log.Println("The token will be reset to retrieve a new one")
default:
log.Fatalln(usage)
}
}
scanner := bufio.NewScanner(os.Stdin)
var username, password, otp string
fmt.Printf("username: ")
scanner.Scan()
username = scanner.Text()
fmt.Printf("password: ")
scanner.Scan()
password = scanner.Text()
fmt.Printf("otp (leave empty if disabled): ")
scanner.Scan()
otp = scanner.Text()
c := api.NewClient(api.EndpointStaging)
if v := os.Getenv("MEGAPORT_ENDPOINT"); v != "" {
endpoint = v
}
log.Printf("Endpoint: %s\n", endpoint)
if username = os.Getenv("MEGAPORT_USERNAME"); username == "" {
log.Fatalln("MEGAPORT_USERNAME is empty")
}
if password = os.Getenv("MEGAPORT_PASSWORD"); password == "" {
log.Fatalln("MEGAPORT_PASSWORD is empty")
}
totpSecret = os.Getenv("MEGAPORT_TOTP_SECRET")
token, err := getToken(endpoint, username, password, totpSecret, reset)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("MEGAPORT_TOKEN=%s\n", token)
}

func getToken(endpoint, username, password, totpSecret string, reset bool) (string, error) {
otp := ""
if totpSecret != "" {
v, err := totp.GenerateCode(totpSecret, time.Now())
if err != nil {
return "", err
}
otp = v
}
c := api.NewClient(endpoint)
if reset {
if err := c.Login(username, password, otp); err != nil {
return "", err
}
if err := c.Logout(); err != nil {
return "", err
}
}
if err := c.Login(username, password, otp); err != nil {
log.Fatal(err)
return "", err
}
fmt.Printf("MEGAPORT_TOKEN=%s\n", c.Token)
return c.Token, nil
}

0 comments on commit 3de9716

Please sign in to comment.