Browse Source

Add admin role

Rufus Deponian 3 years ago
parent
commit
9ca0315f89
3 changed files with 25 additions and 10 deletions
  1. 8 2
      main.go
  2. 3 0
      project_and_roles_template.conf
  3. 14 8
      utils.go

+ 8 - 2
main.go

@@ -91,9 +91,15 @@ func (osclient OpenstackIdentityClient) syncRoleAssignments() {
 			ProjectID: projectID,
 		}
 
-		assignmentResult := roles.Assign(osclient.client, osclient.userRoleID, assignOpts)
+		var roleID string
+		if role.RoleName == "openstack_admin" {
+			roleID = osclient.adminRoleID
+		} else {
+			roleID = osclient.userRoleID
+		}
+		assignmentResult := roles.Assign(osclient.client, roleID, assignOpts)
 		checkErr(assignmentResult.ExtractErr())
-		fmt.Printf("Role %s on project %s assigned to %s\n", osclient.userRoleID, projectID, userID)
+		fmt.Printf("Role %s on project %s assigned to %s\n", roleID, projectID, userID)
 	}
 }
 

+ 3 - 0
project_and_roles_template.conf

@@ -19,5 +19,8 @@ projects_domain_id = domain-id
 # ID роли, которая будет предоставлена пользователям на проектах
 user_role_id = role-id
 
+# ID роли, которая будет предоставлена администраторам на проектах
+admin_role_id = role-id
+
 # Данные о регионе, который будет использоваться при создание compute-клиента
 region = Moscow

+ 14 - 8
utils.go

@@ -41,10 +41,11 @@ func capitalize(str string) string {
 // O P E N S T A C K   C L I E N T
 
 type OpenstackIdentityClient struct {
-	client     *gophercloud.ServiceClient
-	config     *configparser.Configuration
-	domainID   string
-	userRoleID string
+	client      *gophercloud.ServiceClient
+	config      *configparser.Configuration
+	domainID    string
+	userRoleID  string
+	adminRoleID string
 }
 
 func initIdentityClient() OpenstackIdentityClient {
@@ -55,6 +56,7 @@ func initIdentityClient() OpenstackIdentityClient {
 
 	domainID := section.ValueOf("projects_domain_id")
 	userRoleID := section.ValueOf("user_role_id")
+	adminRoleID := section.ValueOf("admin_role_id")
 
 	opts := gophercloud.AuthOptions{
 		IdentityEndpoint: section.ValueOf("identity_endpoint"),
@@ -73,10 +75,11 @@ func initIdentityClient() OpenstackIdentityClient {
 	checkErr(err)
 
 	osclient := OpenstackIdentityClient{
-		client:     client,
-		config:     config,
-		domainID:   domainID,
-		userRoleID: userRoleID,
+		client:      client,
+		config:      config,
+		domainID:    domainID,
+		userRoleID:  userRoleID,
+		adminRoleID: adminRoleID,
 	}
 
 	return osclient
@@ -299,8 +302,11 @@ func getRoles(config *configparser.Configuration) api12Models.Roles {
 	checkErr(err)
 	openstack_users, err := sdApi12.GetRoles(api12Models.Role{ServiceName: "voip", RoleName: "openstack_user"})
 	checkErr(err)
+	openstack_admins, err := sdApi12.GetRoles(api12Models.Role{ServiceName: "voip", RoleName: "openstack_admin"})
+	checkErr(err)
 
 	roles := append(heads, openstack_users...)
+	roles := append(roles, openstack_admins...)
 
 	return roles
 }