Skip to content

Commit

Permalink
chore: check for existing instance in cloud (#3062)
Browse files Browse the repository at this point in the history
* chore: check for existing instance in cloud

* chore: close body

Signed-off-by: Mark Phelps <[email protected]>

---------

Signed-off-by: Mark Phelps <[email protected]>
  • Loading branch information
markphelps authored May 7, 2024
1 parent d7a1440 commit b449f55
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 24 deletions.
85 changes: 61 additions & 24 deletions cmd/flipt/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type cloudInstance struct {
Instance string `json:"instance"`
Organization string `json:"organization"`
Status string `json:"status"`
ExpiresAt int64 `json:"expiresAt"`
ExpiresAt int64 `json:"expiresAt,omitempty"`
}

type cloudError struct {
Expand Down Expand Up @@ -218,32 +218,30 @@ func (c *cloudCommand) serve(cmd *cobra.Command, args []string) error {
fmt.Println("✓ Validated Flipt Cloud authentication.")

if auth.Instance != nil {
// check if instance has not expired
if time.Now().Unix() <= auth.Instance.ExpiresAt {
fmt.Println("✓ Found existing linked Flipt Cloud instance.")
// prompt user to see if they want to use existing instance
ok, err := util.PromptConfirm("Use existing instance?", false)
if err != nil {
return err
}
// check if instance actually exists
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/api/instances/%s/status", c.url, auth.Instance.ID), nil)

if ok {
logger, cfg, err := buildConfig(ctx)
if err != nil {
return err
}
if err != nil {
return fmt.Errorf("creating request: %w", err)
}

cfg.Cloud.Host = u.Hostname()
cfg.Cloud.Instance = auth.Instance.Instance
cfg.Cloud.Organization = auth.Instance.Organization
cfg.Server.Cloud.Enabled = true
cfg.Authentication.Session.Domain = u.Host
req.Header.Set("Authorization", fmt.Sprintf("JWT %s", auth.Token))
req.Header.Set("Accept", "application/json")

fmt.Println("✓ Starting local instance linked with Flipt Cloud.")
return run(ctx, logger, cfg)
}
} else {
fmt.Println("Existing linked Flipt Cloud instance has expired.")
client := &http.Client{
Timeout: 30 * time.Second,
}

resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("making request: %w", err)
}

_ = resp.Body.Close()

switch resp.StatusCode {
case http.StatusNotFound:
fmt.Println("Existing linked Flipt Cloud instance not found.")
ok, err := util.PromptConfirm("Continue with new instance?", false)
if err != nil {
return err
Expand All @@ -252,6 +250,45 @@ func (c *cloudCommand) serve(cmd *cobra.Command, args []string) error {
if !ok {
return nil
}

case http.StatusOK:
// check if instance has not expired
if time.Now().Unix() <= auth.Instance.ExpiresAt {
fmt.Println("✓ Found existing linked Flipt Cloud instance.")
// prompt user to see if they want to use existing instance
ok, err := util.PromptConfirm("Use existing instance?", false)
if err != nil {
return err
}

if ok {
logger, cfg, err := buildConfig(ctx)
if err != nil {
return err
}

cfg.Cloud.Host = u.Hostname()
cfg.Cloud.Instance = auth.Instance.Instance
cfg.Cloud.Organization = auth.Instance.Organization
cfg.Server.Cloud.Enabled = true
cfg.Authentication.Session.Domain = u.Host

fmt.Println("✓ Starting local instance linked with Flipt Cloud.")
return run(ctx, logger, cfg)
}
} else {
fmt.Println("Existing linked Flipt Cloud instance has expired.")
ok, err := util.PromptConfirm("Continue with new instance?", false)
if err != nil {
return err
}

if !ok {
return nil
}
}
default:
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
}

Expand Down
2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ cloud.google.com/go/language v1.12.3/go.mod h1:evFX9wECX6mksEva8RbRnr/4wi/vKGYnA
cloud.google.com/go/lifesciences v0.9.5/go.mod h1:OdBm0n7C0Osh5yZB7j9BXyrMnTRGBJIZonUMxo5CzPw=
cloud.google.com/go/logging v1.9.0/go.mod h1:1Io0vnZv4onoUnsVUQY3HZ3Igb1nBchky0A0y7BBBhE=
cloud.google.com/go/longrunning v0.5.4/go.mod h1:zqNVncI0BOP8ST6XQD1+VcvuShMmq7+xFSzOL++V0dI=
cloud.google.com/go/longrunning v0.5.5 h1:GOE6pZFdSrTb4KAiKnXsJBtlE6mEyaW44oKyMILWnOg=
cloud.google.com/go/longrunning v0.5.5/go.mod h1:WV2LAxD8/rg5Z1cNW6FJ/ZpX4E4VnDnoTk0yawPBB7s=
cloud.google.com/go/managedidentities v1.6.5/go.mod h1:fkFI2PwwyRQbjLxlm5bQ8SjtObFMW3ChBGNqaMcgZjI=
cloud.google.com/go/maps v1.7.0/go.mod h1:LqhDUCNSDAH6MvvubxDm7lPfHPhn6MduIaGCZ4iY1Ig=
Expand Down Expand Up @@ -111,6 +112,7 @@ cloud.google.com/go/security v1.15.5/go.mod h1:KS6X2eG3ynWjqcIX976fuToN5juVkF6Ra
cloud.google.com/go/securitycenter v1.27.0/go.mod h1:J8BkC1KQ59pzVwFZ9OiqcKL57JwPNEv8SMxfVztjqD0=
cloud.google.com/go/servicedirectory v1.11.4/go.mod h1:Bz2T9t+/Ehg6x+Y7Ycq5xiShYLD96NfEsWNHyitj1qM=
cloud.google.com/go/shell v1.7.5/go.mod h1:hL2++7F47/IfpfTO53KYf1EC+F56k3ThfNEXd4zcuiE=
cloud.google.com/go/spanner v1.58.0 h1:W0SAtTz50XIZlTCJtHJ7x4Yxux6ZsorBBxjdXfjZ7UQ=
cloud.google.com/go/spanner v1.58.0/go.mod h1:eSpP+aaT38sdwFs0ovo7Z1VsG9RyM9n7EHpM5DhDEsE=
cloud.google.com/go/speech v1.22.0/go.mod h1:d7pmrSKyrD12c7dRrjqgA/U0eeyZs0i4VpvOlpJXEBA=
cloud.google.com/go/storage v1.35.1/go.mod h1:M6M/3V/D3KpzMTJyPOR/HU6n2Si5QdaXYEsng2xgOs8=
Expand Down

0 comments on commit b449f55

Please sign in to comment.