-
Notifications
You must be signed in to change notification settings - Fork 263
/
cloud_metadata.go
75 lines (61 loc) · 2.11 KB
/
cloud_metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package cloud
import (
"context"
"fmt"
"strings"
steampipecloud "github.com/turbot/steampipe-cloud-sdk-go"
"github.com/turbot/steampipe/pkg/steampipeconfig"
"github.com/turbot/steampipe/sperr"
)
func GetCloudMetadata(ctx context.Context, workspaceDatabaseString, token string) (*steampipeconfig.CloudMetadata, error) {
client := newSteampipeCloudClient(token)
parts := strings.Split(workspaceDatabaseString, "/")
if len(parts) != 2 {
return nil, sperr.New("invalid 'workspace-database' argument '%s' - must be either a connection string or in format <identity>/<workspace>", workspaceDatabaseString)
}
identityHandle := parts[0]
workspaceHandle := parts[1]
// get the identity
identity, _, err := client.Identities.Get(ctx, identityHandle).Execute()
if err != nil {
return nil, sperr.Wrap(err)
}
// get the workspace
var cloudWorkspace steampipecloud.Workspace
if identity.Type == "user" {
cloudWorkspace, _, err = client.UserWorkspaces.Get(ctx, identityHandle, workspaceHandle).Execute()
} else {
cloudWorkspace, _, err = client.OrgWorkspaces.Get(ctx, identityHandle, workspaceHandle).Execute()
}
if err != nil {
return nil, sperr.Wrap(err)
}
workspaceHost := cloudWorkspace.GetHost()
databaseName := cloudWorkspace.GetDatabaseName()
actor, _, err := client.Actors.Get(ctx).Execute()
if err != nil {
return nil, sperr.Wrap(err)
}
password, _, err := client.Users.GetDBPassword(ctx, actor.GetHandle()).Execute()
if err != nil {
return nil, sperr.Wrap(err)
}
connectionString := fmt.Sprintf("postgresql://%s:%s@%s-%s.%s:9193/%s", actor.Handle, password.Password, identityHandle, workspaceHandle, workspaceHost, databaseName)
cloudMetadata := &steampipeconfig.CloudMetadata{
Actor: &steampipeconfig.ActorMetadata{
Id: actor.Id,
Handle: actor.Handle,
},
Identity: &steampipeconfig.IdentityMetadata{
Id: cloudWorkspace.IdentityId,
Type: identity.Type,
Handle: identityHandle,
},
WorkspaceDatabase: &steampipeconfig.WorkspaceMetadata{
Id: cloudWorkspace.Id,
Handle: cloudWorkspace.Handle,
},
ConnectionString: connectionString,
}
return cloudMetadata, nil
}