diff --git a/cce/cce.go b/cce/cce.go index 643060a5..81e421bd 100644 --- a/cce/cce.go +++ b/cce/cce.go @@ -40,44 +40,9 @@ func GetKubeConfig(configParams KubeConfigParams) { println(fmt.Sprintf("Successfully fetched and merge kube config for cce cluster %s.", configParams.ClusterName)) } -func GetProjectsInActiveCloud() { - projectsResponse := getProjectsFromServiceProvider() - var projects config.Projects - for _, project := range projectsResponse.Projects { - projects = append(projects, config.Project{ - NameAndIdResource: config.NameAndIdResource{Name: project.Name, Id: project.Id}, - }) - } - - config.UpdateProjects(projects) - println(fmt.Sprintf("Projects for active cloud:\n%s", strings.Join(projects.GetProjectNames(), ",\n"))) -} - -func getProjectsFromServiceProvider() (projectsResponse common.ProjectsResponse) { - cloud := config.GetActiveCloudConfig() - println(fmt.Sprintf("info: fetching projects for cloud %s", cloud.Domain.Name)) - - request := common.GetRequest(http.MethodGet, endpoints.IamProjects, nil) - request.Header.Add(headers.ContentType, headervalues.ApplicationJson) - request.Header.Add(xheaders.XAuthToken, cloud.UnscopedToken.Secret) - - response := common.HttpClientMakeRequest(request) - bodyBytes := common.GetBodyBytesFromResponse(response) - projectsResponse = *common.DeserializeJsonForType[common.ProjectsResponse](bodyBytes) - - return projectsResponse -} - func getClustersForProjectFromServiceProvider(projectName string) common.ClustersResponse { clustersResponse := common.ClustersResponse{} - cloud := config.GetActiveCloudConfig() - project := cloud.Projects.FindProjectByName(projectName) - if project == nil { - GetProjectsInActiveCloud() - cloud = config.GetActiveCloudConfig() - verifiedProject := cloud.Projects.GetProjectByNameOrThrow(projectName) - project = &verifiedProject - } + project := config.GetActiveCloudConfig().Projects.GetProjectByNameOrThrow(projectName) err := retry.Do( func() error { diff --git a/iam/projects.go b/iam/projects.go new file mode 100644 index 00000000..034653bf --- /dev/null +++ b/iam/projects.go @@ -0,0 +1,48 @@ +package iam + +import ( + "fmt" + "github.com/go-http-utils/headers" + "net/http" + "otc-auth/common" + "otc-auth/common/endpoints" + "otc-auth/common/headervalues" + "otc-auth/common/xheaders" + "otc-auth/config" + "strings" +) + +func GetProjectsInActiveCloud() config.Projects { + projectsResponse := getProjectsFromServiceProvider() + var projects config.Projects + for _, project := range projectsResponse.Projects { + projects = append(projects, config.Project{ + NameAndIdResource: config.NameAndIdResource{Name: project.Name, Id: project.Id}, + }) + } + + config.UpdateProjects(projects) + println(fmt.Sprintf("Projects for active cloud:\n%s", strings.Join(projects.GetProjectNames(), ",\n"))) + return projects +} + +func CreateScopedTokenForEveryProject(projectNames []string) { + for _, projectName := range projectNames { + GetScopedToken(projectName) + } +} + +func getProjectsFromServiceProvider() (projectsResponse common.ProjectsResponse) { + cloud := config.GetActiveCloudConfig() + println(fmt.Sprintf("info: fetching projects for cloud %s", cloud.Domain.Name)) + + request := common.GetRequest(http.MethodGet, endpoints.IamProjects, nil) + request.Header.Add(headers.ContentType, headervalues.ApplicationJson) + request.Header.Add(xheaders.XAuthToken, cloud.UnscopedToken.Secret) + + response := common.HttpClientMakeRequest(request) + bodyBytes := common.GetBodyBytesFromResponse(response) + projectsResponse = *common.DeserializeJsonForType[common.ProjectsResponse](bodyBytes) + + return projectsResponse +} diff --git a/login.go b/login.go index ade8be85..31b969bd 100644 --- a/login.go +++ b/login.go @@ -39,9 +39,15 @@ func AuthenticateAndGetUnscopedToken(authInfo common.AuthInfo) { common.OutputErrorMessageToConsoleAndExit("Authorization did not succeed. Please try again.") } updateOTCInfoFile(tokenResponse) + createScopedTokenForEveryProject() println("Successfully obtained unscoped token!") } +func createScopedTokenForEveryProject() { + projectsInActiveCloud := iam.GetProjectsInActiveCloud() + iam.CreateScopedTokenForEveryProject(projectsInActiveCloud.GetProjectNames()) +} + func updateOTCInfoFile(tokenResponse common.TokenResponse) { cloud := config.GetActiveCloudConfig() if cloud.Domain.Name != tokenResponse.Token.User.Domain.Name { diff --git a/main.go b/main.go index 077217cb..af7eee1d 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "otc-auth/cce" "otc-auth/common" "otc-auth/config" + "otc-auth/iam" "otc-auth/openstack" ) @@ -181,7 +182,7 @@ func main() { } if listProjectsCommand.Happened() { - cce.GetProjectsInActiveCloud() + iam.GetProjectsInActiveCloud() } if cceCommand.Happened() { diff --git a/openstack/openstack.go b/openstack/openstack.go index db4d76dd..a8a08338 100644 --- a/openstack/openstack.go +++ b/openstack/openstack.go @@ -6,31 +6,20 @@ import ( "otc-auth/common" "otc-auth/common/endpoints" "otc-auth/config" - "otc-auth/iam" "path" ) func WriteOpenStackCloudsYaml(openStackConfigFileLocation string) { cloudConfig := config.GetActiveCloudConfig() domainName := cloudConfig.Domain.Name - println("info: will get a scoped token for every project from domain=" + domainName) - createScopedTokenForEveryProject(cloudConfig.Projects.GetProjectNames()) - - updatedCloudConfig := config.GetActiveCloudConfig() clouds := make(map[string]clientconfig.Cloud) - for _, project := range updatedCloudConfig.Projects { + for _, project := range cloudConfig.Projects { cloudName := domainName + "_" + project.Name clouds[cloudName] = createOpenstackCloudConfig(project, domainName) } createOpenstackCloudsYAML(clientconfig.Clouds{Clouds: clouds}, openStackConfigFileLocation) } -func createScopedTokenForEveryProject(projectNames []string) { - for _, projectName := range projectNames { - iam.GetScopedToken(projectName) - } -} - func createOpenstackCloudConfig(project config.Project, domainName string) clientconfig.Cloud { projectName := project.Name cloudName := domainName + "_" + projectName