Skip to content

Commit

Permalink
remove org short name (#1532)
Browse files Browse the repository at this point in the history
* remove org short name

* fix lint and tests

* fix lint and tests
  • Loading branch information
sunkickr authored and kushalmalani committed Feb 7, 2024
1 parent 4b88799 commit c0d76e0
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 459 deletions.
2 changes: 1 addition & 1 deletion cloud/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func CheckUserSession(c *config.Context, coreClient astrocore.CoreClient, platfo
if activeOrg.Product != nil {
orgProduct = fmt.Sprintf("%s", *activeOrg.Product) //nolint
}
err = c.SetOrganizationContext(activeOrg.Id, activeOrg.Name, orgProduct)
err = c.SetOrganizationContext(activeOrg.Id, orgProduct)
if err != nil {
return err
}
Expand Down
19 changes: 9 additions & 10 deletions cloud/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ var (
)

const (
org = "test-org-id"
ws = "workspace-id"
dagDeploy = "disable"
region = "us-central1"
mockOrgShortName = "test-org-short-name"
org = "test-org-id"
ws = "workspace-id"
dagDeploy = "disable"
region = "us-central1"
mockOrgID = "test-org-id"
)

var (
Expand Down Expand Up @@ -1138,7 +1138,7 @@ func TestSelectCluster(t *testing.T) {
t.Run("list cluster failure", func(t *testing.T) {
mockPlatformCoreClient.On("ListClustersWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&astroplatformcore.ListClustersResponse{}, errMock).Once()

_, err := selectCluster("", mockOrgShortName, mockPlatformCoreClient)
_, err := selectCluster("", mockOrgID, mockPlatformCoreClient)
assert.ErrorIs(t, err, errMock)
mockPlatformCoreClient.AssertExpectations(t)
})
Expand All @@ -1162,7 +1162,7 @@ func TestSelectCluster(t *testing.T) {
defer func() { os.Stdin = stdin }()
os.Stdin = r

resp, err := selectCluster("", mockOrgShortName, mockPlatformCoreClient)
resp, err := selectCluster("", mockOrgID, mockPlatformCoreClient)
assert.NoError(t, err)
assert.Equal(t, csID, resp)
})
Expand All @@ -1186,14 +1186,14 @@ func TestSelectCluster(t *testing.T) {
defer func() { os.Stdin = stdin }()
os.Stdin = r

_, err = selectCluster("", mockOrgShortName, mockPlatformCoreClient)
_, err = selectCluster("", mockOrgID, mockPlatformCoreClient)
assert.ErrorIs(t, err, ErrInvalidClusterKey)
})

t.Run("not able to find cluster", func(t *testing.T) {
mockPlatformCoreClient.On("ListClustersWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&mockListClustersResponse, nil).Once()

_, err := selectCluster("test-invalid-id", mockOrgShortName, mockPlatformCoreClient)
_, err := selectCluster("test-invalid-id", mockOrgID, mockPlatformCoreClient)
assert.Error(t, err)
assert.Contains(t, err.Error(), "unable to find specified Cluster")
})
Expand Down Expand Up @@ -1221,7 +1221,6 @@ func TestCanCiCdDeploy(t *testing.T) {
permissions = []string{
"workspaceId:workspace-id",
"organizationId:org-ID",
"orgShortName:org-short-name",
}
mockClaims = util.CustomClaims{
Permissions: permissions,
Expand Down
8 changes: 4 additions & 4 deletions cloud/deployment/fromfile/fromfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

const (
mockOrgShortName = "test-org-short-name"
mockOrgID = "test-org-id"
)

var (
Expand Down Expand Up @@ -3068,14 +3068,14 @@ func TestGetClusterFromName(t *testing.T) {
clusterName = "test-cluster"
t.Run("returns a cluster id if cluster exists in organization", func(t *testing.T) {
mockPlatformCoreClient.On("ListClustersWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&mockListClustersResponse, nil).Once()
actualClusterID, actualNodePools, err = getClusterInfoFromName(clusterName, mockOrgShortName, mockPlatformCoreClient)
actualClusterID, actualNodePools, err = getClusterInfoFromName(clusterName, mockOrgID, mockPlatformCoreClient)
assert.NoError(t, err)
assert.Equal(t, expectedClusterID, actualClusterID)
mockPlatformCoreClient.AssertExpectations(t)
})
t.Run("returns error from api if listing cluster fails", func(t *testing.T) {
mockPlatformCoreClient.On("ListClustersWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&astroplatformcore.ListClustersResponse{}, errTest).Once()
actualClusterID, actualNodePools, err = getClusterInfoFromName(clusterName, mockOrgShortName, mockPlatformCoreClient)
actualClusterID, actualNodePools, err = getClusterInfoFromName(clusterName, mockOrgID, mockPlatformCoreClient)
assert.ErrorIs(t, err, errTest)
assert.Equal(t, "", actualClusterID)
assert.Equal(t, []astroplatformcore.NodePool(nil), actualNodePools)
Expand All @@ -3091,7 +3091,7 @@ func TestGetClusterFromName(t *testing.T) {
},
}
mockPlatformCoreClient.On("ListClustersWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&mockListClustersResponse, nil).Once()
actualClusterID, actualNodePools, err = getClusterInfoFromName(clusterName, mockOrgShortName, mockPlatformCoreClient)
actualClusterID, actualNodePools, err = getClusterInfoFromName(clusterName, mockOrgID, mockPlatformCoreClient)
assert.ErrorIs(t, err, errNotFound)
assert.ErrorContains(t, err, "cluster_name: test-cluster does not exist in organization")
assert.Equal(t, "", actualClusterID)
Expand Down
21 changes: 13 additions & 8 deletions cloud/organization/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func SwitchWithContext(domain string, targetOrg *astroplatformcore.Organization,
if targetOrg.Product != nil {
orgProduct = fmt.Sprintf("%s", *targetOrg.Product) //nolint
}
_ = c.SetOrganizationContext(targetOrg.Id, targetOrg.Name, orgProduct)
_ = c.SetOrganizationContext(targetOrg.Id, orgProduct)
// need to reset all relevant keys because of https://github.com/spf13/viper/issues/1106 :shrug
_ = c.SetContextKey("token", c.Token)
_ = c.SetContextKey("refreshtoken", c.RefreshToken)
Expand Down Expand Up @@ -188,19 +188,24 @@ func Switch(orgNameOrID string, coreClient astrocore.CoreClient, platformCoreCli
// Write the audit logs to the provided io.Writer.
func ExportAuditLogs(coreClient astrocore.CoreClient, platformCoreClient astroplatformcore.CoreClient, orgName, filePath string, earliest int) error {
var orgID string
or, err := ListOrganizations(platformCoreClient)
if err != nil {
return err
}
if orgName == "" {
// get current context
c, err := context.GetCurrentContext()
if err != nil {
return err
}
orgID = c.Organization
orgName = c.OrganizationShortName
} else {
or, err := ListOrganizations(platformCoreClient)
if err != nil {
return err
for i := range or {
if orgID == or[i].Id {
orgName = or[i].Name
break
}
}
} else {
for i := range or {
if orgName == or[i].Name {
orgID = or[i].Id
Expand All @@ -213,11 +218,11 @@ func ExportAuditLogs(coreClient astrocore.CoreClient, platformCoreClient astropl
}
earliestString := fmt.Sprint(earliest)
if filePath == "" {
orgShortName := strings.ReplaceAll(strings.ToLower(orgName), " ", "")
orgName = strings.ReplaceAll(strings.ToLower(orgName), " ", "")

currentTime := time.Now()
date := "-" + currentTime.Format("20060102")
filePath = fmt.Sprintf("%s-logs-%d-day%s%s.ndjson.gz", orgShortName, earliest, pluralize(earliest), date)
filePath = fmt.Sprintf("%s-logs-%d-day%s%s.ndjson.gz", orgName, earliest, pluralize(earliest), date)
}

organizationAuditLogsParams := &astrocore.GetOrganizationAuditLogsParams{
Expand Down
13 changes: 8 additions & 5 deletions cloud/organization/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,15 @@ func TestIsOrgHosted(t *testing.T) {
testUtil.InitTestConfig(testUtil.LocalPlatform)
t.Run("org product is hosted", func(t *testing.T) {
ctx := config.Context{Domain: "localhost"}
ctx.SetOrganizationContext("org1", "org_short_name_1", "HOSTED")
ctx.SetOrganizationContext("org1", "HOSTED")

isHosted := IsOrgHosted()
assert.Equal(t, isHosted, true)
})

t.Run("org product is hybrid", func(t *testing.T) {
ctx := config.Context{Domain: "localhost"}
ctx.SetOrganizationContext("org1", "org_short_name_1", "HYBRID")
ctx.SetOrganizationContext("org1", "HYBRID")

isHosted := IsOrgHosted()
assert.Equal(t, isHosted, false)
Expand All @@ -328,7 +328,7 @@ func TestListClusters(t *testing.T) {
// initialize empty config
testUtil.InitTestConfig(testUtil.LocalPlatform)
mockPlatformCoreClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
orgShortName := "test-org-name"
orgID := "test-org-id"
mockListClustersResponse := astroplatformcore.ListClustersResponse{
HTTPResponse: &http.Response{
StatusCode: 200,
Expand All @@ -349,14 +349,14 @@ func TestListClusters(t *testing.T) {

t.Run("successful list all clusters", func(t *testing.T) {
mockPlatformCoreClient.On("ListClustersWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&mockListClustersResponse, nil).Once()
clusters, err := ListClusters(orgShortName, mockPlatformCoreClient)
clusters, err := ListClusters(orgID, mockPlatformCoreClient)
assert.NoError(t, err)
assert.Equal(t, len(clusters), 2)
})

t.Run("error on listing clusters", func(t *testing.T) {
mockPlatformCoreClient.On("ListClustersWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&astroplatformcore.ListClustersResponse{}, errNetwork).Once()
_, err := ListClusters(orgShortName, mockPlatformCoreClient)
_, err := ListClusters(orgID, mockPlatformCoreClient)
assert.ErrorIs(t, err, errNetwork)
})
}
Expand All @@ -367,6 +367,7 @@ func TestExportAuditLogs(t *testing.T) {
t.Run("export audit logs success", func(t *testing.T) {
mockPlatformClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockPlatformClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockClient.On("GetOrganizationAuditLogsWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&mockOKAuditLogResponse, nil).Once()
err := ExportAuditLogs(mockClient, mockPlatformClient, "", "", 1)
assert.NoError(t, err)
Expand All @@ -386,6 +387,7 @@ func TestExportAuditLogs(t *testing.T) {
t.Run("export failure", func(t *testing.T) {
mockPlatformClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockPlatformClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockClient.On("GetOrganizationAuditLogsWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(nil, errNetwork).Once()
err := ExportAuditLogs(mockClient, mockPlatformClient, "", "", 1)
assert.Contains(t, err.Error(), "network error")
Expand All @@ -404,6 +406,7 @@ func TestExportAuditLogs(t *testing.T) {
t.Run("organization list error", func(t *testing.T) {
mockPlatformClient := new(astroplatformcore_mocks.ClientWithResponsesInterface)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockPlatformClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOKResponse, nil).Once()
mockClient.On("GetOrganizationAuditLogsWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&mockOKAuditLogResponseError, nil).Once()
err := ExportAuditLogs(mockClient, mockPlatformClient, "", "", 1)
assert.Contains(t, err.Error(), "failed to fetch organizations audit logs")
Expand Down
28 changes: 2 additions & 26 deletions cloud/organization/organization_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ func AddOrgTokenToWorkspace(id, name, role, workspace string, out io.Writer, cli
if err != nil {
return err
}
if ctx.OrganizationShortName == "" {
return user.ErrNoShortName
}
if workspace == "" {
workspace = ctx.Workspace
}
Expand Down Expand Up @@ -163,10 +160,6 @@ func getOrganizationTokens(client astrocore.CoreClient) ([]astrocore.ApiToken, e
if err != nil {
return []astrocore.ApiToken{}, err
}
if ctx.OrganizationShortName == "" {
return []astrocore.ApiToken{}, user.ErrNoShortName
}

resp, err := client.ListOrganizationApiTokensWithResponse(httpContext.Background(), ctx.Organization, &astrocore.ListOrganizationApiTokensParams{})
if err != nil {
return []astrocore.ApiToken{}, err
Expand All @@ -181,8 +174,8 @@ func getOrganizationTokens(client astrocore.CoreClient) ([]astrocore.ApiToken, e
return APITokens, nil
}

func getOrganizationTokenByID(id, orgShortName string, client astrocore.CoreClient) (token astrocore.ApiToken, err error) {
resp, err := client.GetOrganizationApiTokenWithResponse(httpContext.Background(), orgShortName, id)
func getOrganizationTokenByID(id, orgID string, client astrocore.CoreClient) (token astrocore.ApiToken, err error) {
resp, err := client.GetOrganizationApiTokenWithResponse(httpContext.Background(), orgID, id)
if err != nil {
return astrocore.ApiToken{}, err
}
Expand Down Expand Up @@ -321,10 +314,6 @@ func CreateToken(name, description, role string, expiration int, cleanOutput boo
if err != nil {
return err
}
if ctx.OrganizationShortName == "" {
return user.ErrNoShortName
}

CreateOrganizationAPITokenRequest := astrocore.CreateOrganizationApiTokenJSONRequestBody{
Description: &description,
Name: name,
Expand Down Expand Up @@ -359,10 +348,6 @@ func UpdateToken(id, name, newName, description, role string, out io.Writer, cli
if err != nil {
return err
}
if ctx.OrganizationShortName == "" {
return user.ErrNoShortName
}

var token astrocore.ApiToken
if id == "" {
tokens, err := getOrganizationTokens(client)
Expand Down Expand Up @@ -448,10 +433,6 @@ func RotateToken(id, name string, cleanOutput, force bool, out io.Writer, client
if err != nil {
return err
}
if ctx.OrganizationShortName == "" {
return user.ErrNoShortName
}

var token astrocore.ApiToken

if id == "" {
Expand Down Expand Up @@ -507,12 +488,7 @@ func DeleteToken(id, name string, force bool, out io.Writer, client astrocore.Co
if err != nil {
return err
}
if ctx.OrganizationShortName == "" {
return user.ErrNoShortName
}

var token astrocore.ApiToken

if id == "" {
tokens, err := getOrganizationTokens(client)
if err != nil {
Expand Down
Loading

0 comments on commit c0d76e0

Please sign in to comment.