Browse Source

Add getPeople()

Rufus Deponian 3 years ago
parent
commit
d9a21234d9
3 changed files with 39 additions and 35 deletions
  1. 11 26
      main.go
  2. 3 1
      project_and_roles_template.conf
  3. 25 8
      utils.go

+ 11 - 26
main.go

@@ -3,11 +3,12 @@ package main
 import (
 	"fmt"
 
+	"github.com/alyu/configparser"
 	"github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
 )
 
 func main() {
-	osclient := initIdentityClient()
+	//osclient := initIdentityClient()
 
 	// allProjects := osclient.getProjectsMap(domainID)
 	// for _, project := range allProjects {
@@ -18,8 +19,15 @@ func main() {
 	// 	fmt.Println(*unit.BriefName)
 	// }
 
-	fmt.Println("Syncing projects...")
-	osclient.syncProjects()
+	config, err := configparser.Read("./project_and_roles.conf")
+	checkErr(err)
+	people := getPeople(config)
+	for _, person := range people {
+		fmt.Println(person.PersonId.Id)
+	}
+
+	// fmt.Println("Syncing projects...")
+	// osclient.syncProjects()
 	// fmt.Println("Deleting projects...")
 	// osclient.deleteAllProjects()
 	//fmt.Println("Syncing roles...")
@@ -46,29 +54,6 @@ func (osclient OpenstackIdentityClient) syncProjects() {
 	}
 }
 
-func (osclient OpenstackIdentityClient) createOrUpdateParentIDs() {
-	allProjects := osclient.getProjectsMap()
-
-	for _, project := range allProjects {
-		if project.Extra["ParentPersNumber"] == nil {
-			continue
-		}
-		parentPersNumber := int(project.Extra["ParentPersNumber"].(float64))
-		parent, ok := allProjects[parentPersNumber]
-		if !ok {
-			panic(fmt.Sprintf("Cannot find parent for %s", project.Name))
-		}
-		if project.ParentID != parent.ID {
-			updateOpts := projects.UpdateOpts{
-				ParentID: parent.ID,
-			}
-			updateResult := projects.Update(osclient.client, project.ID, updateOpts)
-			checkErr(updateResult.Err)
-			fmt.Printf("Parent ID for %s set", project.Name)
-		}
-	}
-}
-
 func (osclient OpenstackIdentityClient) deleteAllProjects() {
 	allProjects := osclient.getProjectsMap()
 

+ 3 - 1
project_and_roles_template.conf

@@ -1,5 +1,7 @@
 [sd.mephi.ru]
-api_key = Your-API-Key-Here
+api1_key = Your-API-Key-Here
+api11_key = Your-API-Key-Here
+api12_key = Your-API-Key-Here
 
 [openstack]
 identity_endpoint = http://controller-IP-Here:5000/v3

+ 25 - 8
utils.go

@@ -6,12 +6,14 @@ import (
 	"unicode"
 
 	"devel.mephi.ru/dyokunev/go-sdapi/sdApi1"
+	"devel.mephi.ru/dyokunev/go-sdapi/sdApi11"
 	"github.com/alyu/configparser"
 	"github.com/gophercloud/gophercloud"
 	"github.com/gophercloud/gophercloud/openstack"
 	"github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
 
-	models "devel.mephi.ru/dyokunev/go-asu-models"
+	asuModels "devel.mephi.ru/dyokunev/go-asu-models"
+	api11Models "devel.mephi.ru/dyokunev/go-sd-models/api11"
 )
 
 // H E L P E R S
@@ -103,7 +105,7 @@ func (osclient OpenstackIdentityClient) getProjectsMap() map[int]projects.Projec
 	return projectsToMap(allProjects)
 }
 
-func makeUpdateOpts(unit models.Unit, domainID string) projects.UpdateOpts {
+func makeUpdateOpts(unit asuModels.Unit, domainID string) projects.UpdateOpts {
 	updateOpts := projects.UpdateOpts{
 		DomainID:    domainID,
 		Enabled:     pointerFromBool(true),
@@ -117,7 +119,7 @@ func makeUpdateOpts(unit models.Unit, domainID string) projects.UpdateOpts {
 	return updateOpts
 }
 
-func makeCreateOpts(unit models.Unit, domainID string) projects.CreateOpts {
+func makeCreateOpts(unit asuModels.Unit, domainID string) projects.CreateOpts {
 	createOpts := projects.CreateOpts{
 		DomainID:    domainID,
 		Enabled:     pointerFromBool(true),
@@ -134,11 +136,11 @@ func makeCreateOpts(unit models.Unit, domainID string) projects.CreateOpts {
 
 // U N I T S
 
-func getAllUnits(config *configparser.Configuration) models.Units {
+func getAllUnits(config *configparser.Configuration) asuModels.Units {
 	section, err := config.Section("sd.mephi.ru")
 	checkErr(err)
 
-	sdApi1.SetApiKey(section.ValueOf("api_key"))
+	sdApi1.SetApiKey(section.ValueOf("api1_key"))
 
 	allUnits, err := sdApi1.GetUnits()
 	checkErr(err)
@@ -146,10 +148,11 @@ func getAllUnits(config *configparser.Configuration) models.Units {
 	return allUnits
 }
 
-func getCleanUnitsMap(config *configparser.Configuration) map[int]models.Unit {
+// Map of persNumber to Unit
+func getCleanUnitsMap(config *configparser.Configuration) map[int]asuModels.Unit {
 	allUnits := getAllUnits(config)
 	idToPersNum := map[int]int{}
-	cleanUnits := map[int]models.Unit{}
+	cleanUnits := map[int]asuModels.Unit{}
 
 	// Get all not nil units without duplicates
 	for _, unit := range allUnits {
@@ -183,7 +186,7 @@ func getCleanUnitsMap(config *configparser.Configuration) map[int]models.Unit {
 	return cleanUnits
 }
 
-func newestUnit(unit1 models.Unit, unit2 models.Unit) models.Unit {
+func newestUnit(unit1 asuModels.Unit, unit2 asuModels.Unit) asuModels.Unit {
 	if unit1.CreateOrderDate != nil && unit2.CreateOrderDate != nil {
 		if unit1.CreateOrderDate.Unix() > unit2.CreateOrderDate.Unix() {
 			return unit1
@@ -193,3 +196,17 @@ func newestUnit(unit1 models.Unit, unit2 models.Unit) models.Unit {
 	return unit1
 
 }
+
+// P E O P L E
+
+func getPeople(config *configparser.Configuration) api11Models.People {
+	section, err := config.Section("sd.mephi.ru")
+	checkErr(err)
+
+	sdApi11.SetApiKey(section.ValueOf("api11_key"))
+
+	people, err := sdApi11.GetPeople()
+	checkErr(err)
+
+	return people
+}