Skip to content

Commit

Permalink
lookup cloud_id if not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Apr 6, 2021
1 parent 24ad522 commit 898ec6b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
41 changes: 37 additions & 4 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Args struct {
ClientSecret string `envconfig:"PLUGIN_CLIENT_SECRET"`

// Site Name (optional)
Site string `envconfig:"PLUGIN_SITE"`
Site string `envconfig:"PLUGIN_INSTANCE"`

// Project Name (required)
Project string `envconfig:"PLUGIN_PROJECT"`
Expand All @@ -66,12 +66,14 @@ func Exec(ctx context.Context, args Args) error {
state = toState(args)
version = toVersion(args)
deeplink = toLink(args)
instance = args.Site
)

logger := logrus.
WithField("client_id", args.ClientID).
WithField("cloud_id", args.CloudID).
WithField("project_id", args.Project).
WithField("instance", instance).
WithField("pipeline", args.Name).
WithField("environment", environ).
WithField("state", state).
Expand Down Expand Up @@ -115,14 +117,29 @@ func Exec(ctx context.Context, args Args) error {
},
}

logrus.Debugln("creating token")
if instance != "" {
logger.Debugln("retrieve cloud id")

tenant, err := lookupTenant(instance)
if err != nil {
logger.WithError(err).
Errorln("cannot retrieve cloud_id")
return err
}
// HACK: we should avoid mutating args
args.CloudID = tenant.ID
logger = logger.WithField("cloud_id", tenant.ID)
logger.Debugln("successfully retrieved cloud id")
}

logger.Debugln("creating token")
token, err := createToken(args)
if err != nil {
logrus.Debugln("cannot create token")
logger.Debugln("cannot create token")
return err
}

logrus.Debugln("creating deployment")
logger.Infoln("creating deployment")
return createDeployment(args, payload, token)
}

Expand Down Expand Up @@ -185,3 +202,19 @@ func createDeployment(args Args, payload Payload, token string) error {
}
return nil
}

// makes an API call to lookup the cloud ID
func lookupTenant(tenant string) (*Tenant, error) {
uri := fmt.Sprintf("https://%s.atlassian.net/_edge/tenant_info", tenant)
res, err := http.Get(uri)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode > 299 {
return nil, fmt.Errorf("Error code %d", res.StatusCode)
}
out := new(Tenant)
err = json.NewDecoder(res.Body).Decode(out)
return out, err
}
5 changes: 5 additions & 0 deletions plugin/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ type (
Displayname string `json:"displayName"`
URL string `json:"url"`
}

// Tenant provides the jira instance tenant details.
Tenant struct {
ID string `json:"cloudId"`
}
)

0 comments on commit 898ec6b

Please sign in to comment.