From 3162afc3a8884642a624a5767290b31f95ff1ec5 Mon Sep 17 00:00:00 2001 From: David Koenitzer Date: Thu, 18 Jan 2024 13:03:23 -0500 Subject: [PATCH] fix commands (#1491) --- cloud/organization/organization_token.go | 32 +++++++++++-------- cloud/team/team.go | 24 +++++++------- cloud/user/user.go | 16 +++++----- cloud/workspace/workspace.go | 6 ++-- cloud/workspace/workspace_token.go | 40 +++++++++++++++--------- 5 files changed, 68 insertions(+), 50 deletions(-) diff --git a/cloud/organization/organization_token.go b/cloud/organization/organization_token.go index af20c3db6..f502713c1 100644 --- a/cloud/organization/organization_token.go +++ b/cloud/organization/organization_token.go @@ -76,7 +76,7 @@ func AddOrgTokenToWorkspace(id, name, role, workspace string, out io.Writer, cli return err } } else { - token, err = getOrganizationTokenByID(id, ctx.OrganizationShortName, client) + token, err = getOrganizationTokenByID(id, ctx.Organization, client) if err != nil { return err } @@ -110,7 +110,7 @@ func AddOrgTokenToWorkspace(id, name, role, workspace string, out io.Writer, cli Roles: updateOrganizationAPITokenRoles, } - resp, err := client.UpdateOrganizationApiTokenWithResponse(httpContext.Background(), ctx.OrganizationShortName, apiTokenID, updateOrganizationAPITokenRequest) + resp, err := client.UpdateOrganizationApiTokenWithResponse(httpContext.Background(), ctx.Organization, apiTokenID, updateOrganizationAPITokenRequest) if err != nil { return err } @@ -167,7 +167,7 @@ func getOrganizationTokens(client astrocore.CoreClient) ([]astrocore.ApiToken, e return []astrocore.ApiToken{}, user.ErrNoShortName } - resp, err := client.ListOrganizationApiTokensWithResponse(httpContext.Background(), ctx.OrganizationShortName, &astrocore.ListOrganizationApiTokensParams{}) + resp, err := client.ListOrganizationApiTokensWithResponse(httpContext.Background(), ctx.Organization, &astrocore.ListOrganizationApiTokensParams{}) if err != nil { return []astrocore.ApiToken{}, err } @@ -259,8 +259,14 @@ func ListTokens(client astrocore.CoreClient, out io.Writer) error { } } created := TimeAgo(apiTokens[i].CreatedAt) - createdBy := apiTokens[i].CreatedBy.FullName - tab.AddRow([]string{id, name, description, string(scope), role, created, *createdBy}, false) + var createdBy string + switch { + case apiTokens[i].CreatedBy.FullName != nil: + createdBy = *apiTokens[i].CreatedBy.FullName + case apiTokens[i].CreatedBy.ApiTokenName != nil: + createdBy = *apiTokens[i].CreatedBy.ApiTokenName + } + tab.AddRow([]string{id, name, description, string(scope), role, created, createdBy}, false) } tab.Print(out) @@ -284,7 +290,7 @@ func ListTokenRoles(id string, client astrocore.CoreClient, out io.Writer) (err if err != nil { return err } - apiToken, err = getOrganizationTokenByID(id, ctx.OrganizationShortName, client) + apiToken, err = getOrganizationTokenByID(id, ctx.Organization, client) if err != nil { return err } @@ -327,7 +333,7 @@ func CreateToken(name, description, role string, expiration int, cleanOutput boo if expiration != 0 { CreateOrganizationAPITokenRequest.TokenExpiryPeriodInDays = &expiration } - resp, err := client.CreateOrganizationApiTokenWithResponse(httpContext.Background(), ctx.OrganizationShortName, CreateOrganizationAPITokenRequest) + resp, err := client.CreateOrganizationApiTokenWithResponse(httpContext.Background(), ctx.Organization, CreateOrganizationAPITokenRequest) if err != nil { return err } @@ -368,7 +374,7 @@ func UpdateToken(id, name, newName, description, role string, out io.Writer, cli return err } } else { - token, err = getOrganizationTokenByID(id, ctx.OrganizationShortName, client) + token, err = getOrganizationTokenByID(id, ctx.Organization, client) if err != nil { return err } @@ -424,7 +430,7 @@ func UpdateToken(id, name, newName, description, role string, out io.Writer, cli UpdateOrganizationAPITokenRequest.Roles = updateOrganizationAPITokenRoles } - resp, err := client.UpdateOrganizationApiTokenWithResponse(httpContext.Background(), ctx.OrganizationShortName, apiTokenID, UpdateOrganizationAPITokenRequest) + resp, err := client.UpdateOrganizationApiTokenWithResponse(httpContext.Background(), ctx.Organization, apiTokenID, UpdateOrganizationAPITokenRequest) if err != nil { return err } @@ -458,7 +464,7 @@ func RotateToken(id, name string, cleanOutput, force bool, out io.Writer, client return err } } else { - token, err = getOrganizationTokenByID(id, ctx.OrganizationShortName, client) + token, err = getOrganizationTokenByID(id, ctx.Organization, client) if err != nil { return err } @@ -475,7 +481,7 @@ func RotateToken(id, name string, cleanOutput, force bool, out io.Writer, client return nil } } - resp, err := client.RotateOrganizationApiTokenWithResponse(httpContext.Background(), ctx.OrganizationShortName, apiTokenID) + resp, err := client.RotateOrganizationApiTokenWithResponse(httpContext.Background(), ctx.Organization, apiTokenID) if err != nil { return err } @@ -517,7 +523,7 @@ func DeleteToken(id, name string, force bool, out io.Writer, client astrocore.Co return err } } else { - token, err = getOrganizationTokenByID(id, ctx.OrganizationShortName, client) + token, err = getOrganizationTokenByID(id, ctx.Organization, client) if err != nil { return err } @@ -546,7 +552,7 @@ func DeleteToken(id, name string, force bool, out io.Writer, client astrocore.Co } } - resp, err := client.DeleteOrganizationApiTokenWithResponse(httpContext.Background(), ctx.OrganizationShortName, apiTokenID) + resp, err := client.DeleteOrganizationApiTokenWithResponse(httpContext.Background(), ctx.Organization, apiTokenID) if err != nil { return err } diff --git a/cloud/team/team.go b/cloud/team/team.go index 097432fc3..00667ab76 100644 --- a/cloud/team/team.go +++ b/cloud/team/team.go @@ -62,7 +62,7 @@ func CreateTeam(name, description, role string, out io.Writer, client astrocore. Name: name, OrganizationRole: &role, } - resp, err := client.CreateTeamWithResponse(httpContext.Background(), ctx.OrganizationShortName, teamCreateRequest) + resp, err := client.CreateTeamWithResponse(httpContext.Background(), ctx.Organization, teamCreateRequest) if err != nil { return err } @@ -83,7 +83,7 @@ func GetTeam(client astrocore.CoreClient, teamID string) (team astrocore.Team, e return team, ErrNoShortName } - resp, err := client.GetTeamWithResponse(httpContext.Background(), ctx.OrganizationShortName, teamID) + resp, err := client.GetTeamWithResponse(httpContext.Background(), ctx.Organization, teamID) if err != nil { return team, err } @@ -139,7 +139,7 @@ func UpdateWorkspaceTeamRole(id, role, workspace string, out io.Writer, client a teamID := team.Id teamMutateRequest := astrocore.MutateWorkspaceTeamRoleRequest{Role: role} - resp, err := client.MutateWorkspaceTeamRoleWithResponse(httpContext.Background(), ctx.OrganizationShortName, workspace, teamID, teamMutateRequest) + resp, err := client.MutateWorkspaceTeamRoleWithResponse(httpContext.Background(), ctx.Organization, workspace, teamID, teamMutateRequest) if err != nil { return err } @@ -204,7 +204,7 @@ func UpdateTeam(id, name, description, role string, out io.Writer, client astroc teamUpdateRequest.Description = description } - resp, err := client.UpdateTeamWithResponse(httpContext.Background(), ctx.OrganizationShortName, teamID, teamUpdateRequest) + resp, err := client.UpdateTeamWithResponse(httpContext.Background(), ctx.Organization, teamID, teamUpdateRequest) if err != nil { return err } @@ -220,7 +220,7 @@ func UpdateTeam(id, name, description, role string, out io.Writer, client astroc return err } teamMutateRoleRequest := astrocore.MutateOrgTeamRoleRequest{Role: role} - resp, err := client.MutateOrgTeamRoleWithResponse(httpContext.Background(), ctx.OrganizationShortName, teamID, teamMutateRoleRequest) + resp, err := client.MutateOrgTeamRoleWithResponse(httpContext.Background(), ctx.Organization, teamID, teamMutateRoleRequest) if err != nil { return err } @@ -269,7 +269,7 @@ func RemoveWorkspaceTeam(id, workspace string, out io.Writer, client astrocore.C } } teamID := team.Id - resp, err := client.DeleteWorkspaceTeamWithResponse(httpContext.Background(), ctx.OrganizationShortName, ctx.Workspace, teamID) + resp, err := client.DeleteWorkspaceTeamWithResponse(httpContext.Background(), ctx.Organization, ctx.Workspace, teamID) if err != nil { return err } @@ -324,7 +324,7 @@ func GetWorkspaceTeams(client astrocore.CoreClient, workspace string, limit int) workspace = ctx.Workspace } for { - resp, err := client.ListWorkspaceTeamsWithResponse(httpContext.Background(), ctx.OrganizationShortName, workspace, &astrocore.ListWorkspaceTeamsParams{ + resp, err := client.ListWorkspaceTeamsWithResponse(httpContext.Background(), ctx.Organization, workspace, &astrocore.ListWorkspaceTeamsParams{ Offset: &offset, Limit: &limit, }) @@ -430,7 +430,7 @@ func AddWorkspaceTeam(id, role, workspace string, out io.Writer, client astrocor mutateUserInput := astrocore.MutateWorkspaceTeamRoleRequest{ Role: role, } - resp, err := client.MutateWorkspaceTeamRoleWithResponse(httpContext.Background(), ctx.OrganizationShortName, workspace, teamID, mutateUserInput) + resp, err := client.MutateWorkspaceTeamRoleWithResponse(httpContext.Background(), ctx.Organization, workspace, teamID, mutateUserInput) if err != nil { return err } @@ -455,7 +455,7 @@ func GetOrgTeams(client astrocore.CoreClient) ([]astrocore.Team, error) { } for { - resp, err := client.ListOrganizationTeamsWithResponse(httpContext.Background(), ctx.OrganizationShortName, &astrocore.ListOrganizationTeamsParams{ + resp, err := client.ListOrganizationTeamsWithResponse(httpContext.Background(), ctx.Organization, &astrocore.ListOrganizationTeamsParams{ Offset: &offset, Limit: &teamPagnationLimit, }) @@ -543,7 +543,7 @@ func Delete(id string, out io.Writer, client astrocore.CoreClient) error { } } teamID := team.Id - resp, err := client.DeleteTeamWithResponse(httpContext.Background(), ctx.OrganizationShortName, teamID) + resp, err := client.DeleteTeamWithResponse(httpContext.Background(), ctx.Organization, teamID) if err != nil { return err } @@ -618,7 +618,7 @@ func RemoveUser(teamID, teamMemberID string, out io.Writer, client astrocore.Cor } userID := teamMemberSelection.UserId - resp, err := client.RemoveTeamMemberWithResponse(httpContext.Background(), ctx.OrganizationShortName, teamID, userID) + resp, err := client.RemoveTeamMemberWithResponse(httpContext.Background(), ctx.Organization, teamID, userID) if err != nil { return err } @@ -698,7 +698,7 @@ func AddUser(teamID, userID string, out io.Writer, client astrocore.CoreClient) MemberIds: []string{userID}, } - resp, err := client.AddTeamMembersWithResponse(httpContext.Background(), ctx.OrganizationShortName, teamID, addTeamMembersRequest) + resp, err := client.AddTeamMembersWithResponse(httpContext.Background(), ctx.Organization, teamID, addTeamMembersRequest) if err != nil { return err } diff --git a/cloud/user/user.go b/cloud/user/user.go index 85dce4941..187157803 100644 --- a/cloud/user/user.go +++ b/cloud/user/user.go @@ -52,7 +52,7 @@ func CreateInvite(email, role string, out io.Writer, client astrocore.CoreClient InviteeEmail: email, Role: role, } - resp, err := client.CreateUserInviteWithResponse(httpContext.Background(), ctx.OrganizationShortName, userInviteInput) + resp, err := client.CreateUserInviteWithResponse(httpContext.Background(), ctx.Organization, userInviteInput) if err != nil { return err } @@ -106,7 +106,7 @@ func UpdateUserRole(email, role string, out io.Writer, client astrocore.CoreClie mutateUserInput := astrocore.MutateOrgUserRoleRequest{ Role: role, } - resp, err := client.MutateOrgUserRoleWithResponse(httpContext.Background(), ctx.OrganizationShortName, userID, mutateUserInput) + resp, err := client.MutateOrgUserRoleWithResponse(httpContext.Background(), ctx.Organization, userID, mutateUserInput) if err != nil { return err } @@ -192,7 +192,7 @@ func GetOrgUsers(client astrocore.CoreClient) ([]astrocore.User, error) { } for { - resp, err := client.ListOrgUsersWithResponse(httpContext.Background(), ctx.OrganizationShortName, &astrocore.ListOrgUsersParams{ + resp, err := client.ListOrgUsersWithResponse(httpContext.Background(), ctx.Organization, &astrocore.ListOrgUsersParams{ Offset: &offset, Limit: &userPagnationLimit, }) @@ -274,7 +274,7 @@ func AddWorkspaceUser(email, role, workspace string, out io.Writer, client astro mutateUserInput := astrocore.MutateWorkspaceUserRoleRequest{ Role: role, } - resp, err := client.MutateWorkspaceUserRoleWithResponse(httpContext.Background(), ctx.OrganizationShortName, workspace, userID, mutateUserInput) + resp, err := client.MutateWorkspaceUserRoleWithResponse(httpContext.Background(), ctx.Organization, workspace, userID, mutateUserInput) if err != nil { return err } @@ -314,7 +314,7 @@ func UpdateWorkspaceUserRole(email, role, workspace string, out io.Writer, clien Role: role, } fmt.Println("workspace: " + workspace) - resp, err := client.MutateWorkspaceUserRoleWithResponse(httpContext.Background(), ctx.OrganizationShortName, workspace, userID, mutateUserInput) + resp, err := client.MutateWorkspaceUserRoleWithResponse(httpContext.Background(), ctx.Organization, workspace, userID, mutateUserInput) if err != nil { fmt.Println("error in MutateWorkspaceUserRoleWithResponse") return err @@ -370,7 +370,7 @@ func GetWorkspaceUsers(client astrocore.CoreClient, workspace string, limit int) workspace = ctx.Workspace } for { - resp, err := client.ListWorkspaceUsersWithResponse(httpContext.Background(), ctx.OrganizationShortName, workspace, &astrocore.ListWorkspaceUsersParams{ + resp, err := client.ListWorkspaceUsersWithResponse(httpContext.Background(), ctx.Organization, workspace, &astrocore.ListWorkspaceUsersParams{ Offset: &offset, Limit: &limit, }) @@ -439,7 +439,7 @@ func RemoveWorkspaceUser(email, workspace string, out io.Writer, client astrocor if err != nil { return err } - resp, err := client.DeleteWorkspaceUserWithResponse(httpContext.Background(), ctx.OrganizationShortName, workspace, userID) + resp, err := client.DeleteWorkspaceUserWithResponse(httpContext.Background(), ctx.Organization, workspace, userID) if err != nil { return err } @@ -478,7 +478,7 @@ func GetUser(client astrocore.CoreClient, userID string) (user astrocore.User, e return user, ErrNoShortName } - resp, err := client.GetUserWithResponse(httpContext.Background(), ctx.OrganizationShortName, userID) + resp, err := client.GetUserWithResponse(httpContext.Background(), ctx.Organization, userID) if err != nil { return user, err } diff --git a/cloud/workspace/workspace.go b/cloud/workspace/workspace.go index 1e463abd5..a0c58fc6a 100644 --- a/cloud/workspace/workspace.go +++ b/cloud/workspace/workspace.go @@ -198,7 +198,7 @@ func Create(name, description, enforceCD string, out io.Writer, client astrocore Description: &description, Name: name, } - resp, err := client.CreateWorkspaceWithResponse(httpContext.Background(), ctx.OrganizationShortName, workspaceCreateRequest) + resp, err := client.CreateWorkspaceWithResponse(httpContext.Background(), ctx.Organization, workspaceCreateRequest) if err != nil { return err } @@ -265,7 +265,7 @@ func Update(id, name, description, enforceCD string, out io.Writer, client astro } workspaceUpdateRequest.ApiKeyOnlyDeploymentsDefault = enforce } - resp, err := client.UpdateWorkspaceWithResponse(httpContext.Background(), ctx.OrganizationShortName, workspaceID, workspaceUpdateRequest) + resp, err := client.UpdateWorkspaceWithResponse(httpContext.Background(), ctx.Organization, workspaceID, workspaceUpdateRequest) if err != nil { return err } @@ -309,7 +309,7 @@ func Delete(id string, out io.Writer, client astrocore.CoreClient) error { } } workspaceID := workspace.Id - resp, err := client.DeleteWorkspaceWithResponse(httpContext.Background(), ctx.OrganizationShortName, workspaceID) + resp, err := client.DeleteWorkspaceWithResponse(httpContext.Background(), ctx.Organization, workspaceID) if err != nil { return err } diff --git a/cloud/workspace/workspace_token.go b/cloud/workspace/workspace_token.go index a9fad0cda..d7f5b8475 100644 --- a/cloud/workspace/workspace_token.go +++ b/cloud/workspace/workspace_token.go @@ -69,8 +69,14 @@ func ListTokens(client astrocore.CoreClient, workspace string, out io.Writer) er } } created := TimeAgo(apiTokens[i].CreatedAt) - createdBy := apiTokens[i].CreatedBy.FullName - tab.AddRow([]string{id, name, description, string(scope), role, created, *createdBy}, false) + var createdBy string + switch { + case apiTokens[i].CreatedBy.FullName != nil: + createdBy = *apiTokens[i].CreatedBy.FullName + case apiTokens[i].CreatedBy.ApiTokenName != nil: + createdBy = *apiTokens[i].CreatedBy.ApiTokenName + } + tab.AddRow([]string{id, name, description, string(scope), role, created, createdBy}, false) } tab.Print(out) @@ -104,7 +110,7 @@ func CreateToken(name, description, role, workspace string, expiration int, clea if expiration != 0 { CreateWorkspaceAPITokenRequest.TokenExpiryPeriodInDays = &expiration } - resp, err := client.CreateWorkspaceApiTokenWithResponse(httpContext.Background(), ctx.OrganizationShortName, workspace, CreateWorkspaceAPITokenRequest) + resp, err := client.CreateWorkspaceApiTokenWithResponse(httpContext.Background(), ctx.Organization, workspace, CreateWorkspaceAPITokenRequest) if err != nil { return err } @@ -148,7 +154,7 @@ func UpdateToken(id, name, newName, description, role, workspace string, out io. return err } } else { - token, err = getWorkspaceTokenByID(id, workspace, ctx.OrganizationShortName, client) + token, err = getWorkspaceTokenByID(id, workspace, ctx.Organization, client) if err != nil { return err } @@ -189,7 +195,7 @@ func UpdateToken(id, name, newName, description, role, workspace string, out io. UpdateWorkspaceAPITokenRequest.Role = role } - resp, err := client.UpdateWorkspaceApiTokenWithResponse(httpContext.Background(), ctx.OrganizationShortName, workspace, apiTokenID, UpdateWorkspaceAPITokenRequest) + resp, err := client.UpdateWorkspaceApiTokenWithResponse(httpContext.Background(), ctx.Organization, workspace, apiTokenID, UpdateWorkspaceAPITokenRequest) if err != nil { return err } @@ -224,7 +230,7 @@ func RotateToken(id, name, workspace string, cleanOutput, force bool, out io.Wri return err } } else { - token, err = getWorkspaceTokenByID(id, workspace, ctx.OrganizationShortName, client) + token, err = getWorkspaceTokenByID(id, workspace, ctx.Organization, client) if err != nil { return err } @@ -241,7 +247,7 @@ func RotateToken(id, name, workspace string, cleanOutput, force bool, out io.Wri return nil } } - resp, err := client.RotateWorkspaceApiTokenWithResponse(httpContext.Background(), ctx.OrganizationShortName, workspace, apiTokenID) + resp, err := client.RotateWorkspaceApiTokenWithResponse(httpContext.Background(), ctx.Organization, workspace, apiTokenID) if err != nil { return err } @@ -284,7 +290,7 @@ func DeleteToken(id, name, workspace string, force bool, out io.Writer, client a return err } } else { - token, err = getWorkspaceTokenByID(id, workspace, ctx.OrganizationShortName, client) + token, err = getWorkspaceTokenByID(id, workspace, ctx.Organization, client) if err != nil { return err } @@ -313,7 +319,7 @@ func DeleteToken(id, name, workspace string, force bool, out io.Writer, client a } } - resp, err := client.DeleteWorkspaceApiTokenWithResponse(httpContext.Background(), ctx.OrganizationShortName, workspace, apiTokenID) + resp, err := client.DeleteWorkspaceApiTokenWithResponse(httpContext.Background(), ctx.Organization, workspace, apiTokenID) if err != nil { return err } @@ -344,7 +350,13 @@ func selectTokens(workspace string, apiTokens []astrocore.ApiToken) (astrocore.A } } created := TimeAgo(apiTokens[i].CreatedAt) - createdBy := apiTokens[i].CreatedBy.FullName + var createdBy string + switch { + case apiTokens[i].CreatedBy.FullName != nil: + createdBy = *apiTokens[i].CreatedBy.FullName + case apiTokens[i].CreatedBy.ApiTokenName != nil: + createdBy = *apiTokens[i].CreatedBy.ApiTokenName + } index := i + 1 tab.AddRow([]string{ @@ -355,7 +367,7 @@ func selectTokens(workspace string, apiTokens []astrocore.ApiToken) (astrocore.A string(scope), role, created, - *createdBy, + createdBy, }, false) apiTokensMap[strconv.Itoa(index)] = apiTokens[i] } @@ -383,7 +395,7 @@ func getWorkspaceTokens(workspace string, client astrocore.CoreClient) ([]astroc workspace = ctx.Workspace } - resp, err := client.ListWorkspaceApiTokensWithResponse(httpContext.Background(), ctx.OrganizationShortName, workspace, &astrocore.ListWorkspaceApiTokensParams{}) + resp, err := client.ListWorkspaceApiTokensWithResponse(httpContext.Background(), ctx.Organization, workspace, &astrocore.ListWorkspaceApiTokensParams{}) if err != nil { return []astrocore.ApiToken{}, err } @@ -455,8 +467,8 @@ func TimeAgo(date time.Time) string { } } -func getWorkspaceTokenByID(id, workspaceID, orgShortName string, client astrocore.CoreClient) (token astrocore.ApiToken, err error) { - resp, err := client.GetWorkspaceApiTokenWithResponse(httpContext.Background(), orgShortName, workspaceID, id) +func getWorkspaceTokenByID(id, workspaceID, orgID string, client astrocore.CoreClient) (token astrocore.ApiToken, err error) { + resp, err := client.GetWorkspaceApiTokenWithResponse(httpContext.Background(), orgID, workspaceID, id) if err != nil { return astrocore.ApiToken{}, err }