Skip to content

Commit

Permalink
#22 Create Openstack config does not work properly since it requires …
Browse files Browse the repository at this point in the history
…list projects first
  • Loading branch information
Victor Getz authored and victorgetz committed Apr 13, 2023
1 parent 6574982 commit b49d3d6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 49 deletions.
37 changes: 1 addition & 36 deletions cce/cce.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
48 changes: 48 additions & 0 deletions iam/projects.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"otc-auth/cce"
"otc-auth/common"
"otc-auth/config"
"otc-auth/iam"
"otc-auth/openstack"
)

Expand Down Expand Up @@ -181,7 +182,7 @@ func main() {
}

if listProjectsCommand.Happened() {
cce.GetProjectsInActiveCloud()
iam.GetProjectsInActiveCloud()
}

if cceCommand.Happened() {
Expand Down
13 changes: 1 addition & 12 deletions openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b49d3d6

Please sign in to comment.