Ver código fonte

Add domain ID to configuration file

Rufus Deponian 6 anos atrás
pai
commit
7978f519b6
3 arquivos alterados com 32 adições e 29 exclusões
  1. 12 20
      main.go
  2. 5 2
      project_and_roles_template.conf
  3. 15 7
      utils.go

+ 12 - 20
main.go

@@ -3,19 +3,11 @@ package main
 import (
 	"fmt"
 
-	"github.com/alyu/configparser"
 	"github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
 )
 
 func main() {
-	domainID := "614b64b5d4ce4e989ad0590ec56b021f" // mephi domain
-
-	var osclient OpenstackIdentityClient
-	osclient.client = initIdentityClient()
-
-	config, err := configparser.Read("./project_and_roles.conf")
-	checkErr(err)
-	osclient.config = config
+	osclient := initIdentityClient()
 
 	// allProjects := osclient.getProjectsMap(domainID)
 	// for _, project := range allProjects {
@@ -27,26 +19,26 @@ func main() {
 	// }
 
 	fmt.Println("Syncing projects...")
-	osclient.syncProjects(domainID)
+	osclient.syncProjects()
 	// fmt.Println("Deleting projects...")
-	// osclient.deleteAllProjects(domainID)
+	// osclient.deleteAllProjects()
 	//fmt.Println("Syncing roles...")
-	//syncRoles(domainID)
+	//syncRoles()
 }
 
-func (osclient OpenstackIdentityClient) syncProjects(domainID string) {
+func (osclient OpenstackIdentityClient) syncProjects() {
 	allUnits := getCleanUnitsMap(osclient.config)
-	allProjects := osclient.getProjectsMap(domainID)
+	allProjects := osclient.getProjectsMap()
 
 	for persNumber, unit := range allUnits {
 		if _, ok := allProjects[persNumber]; ok {
-			updateOpts := makeUpdateOpts(unit, domainID)
+			updateOpts := makeUpdateOpts(unit, osclient.domainID)
 			projectID := allProjects[persNumber].ID
 			updateResult := projects.Update(osclient.client, projectID, updateOpts)
 			checkErr(updateResult.Err)
 			fmt.Printf("Project \"%s\" updated\n", *unit.BriefName)
 		} else {
-			createOpts := makeCreateOpts(unit, domainID)
+			createOpts := makeCreateOpts(unit, osclient.domainID)
 			createResult := projects.Create(osclient.client, createOpts)
 			checkErr(createResult.Err)
 			fmt.Printf("Project \"%s\" created\n", *unit.BriefName)
@@ -54,8 +46,8 @@ func (osclient OpenstackIdentityClient) syncProjects(domainID string) {
 	}
 }
 
-func (osclient OpenstackIdentityClient) createOrUpdateParentIDs(domainID string) {
-	allProjects := osclient.getProjectsMap(domainID)
+func (osclient OpenstackIdentityClient) createOrUpdateParentIDs() {
+	allProjects := osclient.getProjectsMap()
 
 	for _, project := range allProjects {
 		if project.Extra["ParentPersNumber"] == nil {
@@ -77,8 +69,8 @@ func (osclient OpenstackIdentityClient) createOrUpdateParentIDs(domainID string)
 	}
 }
 
-func (osclient OpenstackIdentityClient) deleteAllProjects(domainID string) {
-	allProjects := osclient.getProjectsMap(domainID)
+func (osclient OpenstackIdentityClient) deleteAllProjects() {
+	allProjects := osclient.getProjectsMap()
 
 	index := 1
 	amount := len(allProjects)

+ 5 - 2
project_and_roles_template.conf

@@ -3,10 +3,13 @@ api_key = Your-API-Key-Here
 
 [openstack]
 identity_endpoint = http://controller-IP-Here:5000/v3
+
+# Данные о пользователе, от лица которого будут выполняться запросы
+# Пользователь должен иметь роль администратора
 domain_name = mephi
 project_name = UserProjectName
-
-# You have to use admin level user here
 username = GeraltFromRivia
 password = Plotva
 
+# ID домена, в котором находятся синхронизируемые проекты
+projects_domain_id = domain-id

+ 15 - 7
utils.go

@@ -35,17 +35,19 @@ 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
+	client   *gophercloud.ServiceClient
+	config   *configparser.Configuration
+	domainID string
 }
 
-func initIdentityClient() *gophercloud.ServiceClient {
+func initIdentityClient() OpenstackIdentityClient {
 	config, err := configparser.Read("./project_and_roles.conf")
 	checkErr(err)
-
 	section, err := config.Section("openstack")
 	checkErr(err)
 
+	domainID := section.ValueOf("projects_domain_id")
+
 	opts := gophercloud.AuthOptions{
 		IdentityEndpoint: section.ValueOf("identity_endpoint"),
 		DomainName:       section.ValueOf("domain_name"),
@@ -62,7 +64,13 @@ func initIdentityClient() *gophercloud.ServiceClient {
 	})
 	checkErr(err)
 
-	return client
+	osclient := OpenstackIdentityClient{
+		client:   client,
+		config:   config,
+		domainID: domainID,
+	}
+
+	return osclient
 }
 
 func projectsToMap(allProjects []projects.Project) map[int]projects.Project {
@@ -79,9 +87,9 @@ func projectsToMap(allProjects []projects.Project) map[int]projects.Project {
 	return projectsMap
 }
 
-func (osclient OpenstackIdentityClient) getProjectsMap(domainID string) map[int]projects.Project {
+func (osclient OpenstackIdentityClient) getProjectsMap() map[int]projects.Project {
 	listOpts := projects.ListOpts{
-		DomainID: domainID,
+		DomainID: osclient.domainID,
 		Enabled:  pointerFromBool(true),
 		IsDomain: pointerFromBool(false),
 	}