Skip to content

Commit

Permalink
Merge pull request #19 from silinternational/develop
Browse files Browse the repository at this point in the history
Release 2.2.0
  • Loading branch information
fillup authored Nov 11, 2019
2 parents 2dabef2 + f2b3118 commit 18a19e4
Show file tree
Hide file tree
Showing 3 changed files with 399 additions and 24 deletions.
148 changes: 125 additions & 23 deletions googledest/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,82 @@ func (g *GoogleUsers) ForSet(syncSetJson json.RawMessage) error {
return nil
}

func extractData(user admin.User) personnel_sync.Person {
newPerson := personnel_sync.Person{
CompareValue: user.PrimaryEmail,
Attributes: map[string]string{
"email": strings.ToLower(user.PrimaryEmail),
},
}

if found := findFirstMatchingType(user.ExternalIds, "organization"); found != nil {
setStringFromInterface(found["value"], newPerson.Attributes, "id")
}

if found := findFirstMatchingType(user.Locations, "desk"); found != nil {
setStringFromInterface(found["area"], newPerson.Attributes, "area")
setStringFromInterface(found["buildingId"], newPerson.Attributes, "building")
}

if found := findFirstMatchingType(user.Organizations, ""); found != nil {
setStringFromInterface(found["costCenter"], newPerson.Attributes, "costCenter")
setStringFromInterface(found["department"], newPerson.Attributes, "department")
setStringFromInterface(found["title"], newPerson.Attributes, "title")
}

if found := findFirstMatchingType(user.Phones, "work"); found != nil {
setStringFromInterface(found["value"], newPerson.Attributes, "phone")
}

if found := findFirstMatchingType(user.Relations, "manager"); found != nil {
setStringFromInterface(found["value"], newPerson.Attributes, "manager")
}

if user.Name != nil {
newPerson.Attributes["familyName"] = user.Name.FamilyName
newPerson.Attributes["givenName"] = user.Name.GivenName
}

return newPerson
}

// findFirstMatchingType iterates through a slice of interfaces until it finds a matching key. The underlying type
// of the given interface must be `[]map[string]interface{}`. If `findType` is empty, the first element in the
// slice is returned.
func findFirstMatchingType(in interface{}, findType string) map[string]interface{} {
sliceOfInterfaces, ok := in.([]interface{})
if !ok {
return nil
}
for _, i := range sliceOfInterfaces {
if i2 := isMatchingType(i, findType); i2 != nil {
return i2
}
}
return nil
}

// isMatchingType returns the value of `i`, cast to `map[string]interface{}` if it contains an entry with key 'type'
// and value equal to `findType`. If `findType` is empty, the first element in the slice is returned.
func isMatchingType(i interface{}, findType string) map[string]interface{} {
if m, ok := i.(map[string]interface{}); ok {
if findType == "" {
return m
}
if recordType, ok := m["type"].(string); ok && recordType == findType {
return m
}
}
return nil
}

// setStringFromInterface gets a string from an interface{}, and assigns it to a map
func setStringFromInterface(i interface{}, m map[string]string, key string) {
if value, ok := i.(string); ok {
m[key] = value
}
}

func (g *GoogleUsers) ListUsers() ([]personnel_sync.Person, error) {
var usersList []*admin.User
usersListCall := g.AdminService.Users.List()
Expand All @@ -68,26 +144,16 @@ func (g *GoogleUsers) ListUsers() ([]personnel_sync.Person, error) {
return nil
})
if err != nil {
return []personnel_sync.Person{}, fmt.Errorf("unable to get users: %s", err)
return nil, fmt.Errorf("unable to get users: %s", err)
}

var users []personnel_sync.Person

var people []personnel_sync.Person
for _, nextUser := range usersList {
newPerson := personnel_sync.Person{
CompareValue: nextUser.PrimaryEmail,
Attributes: map[string]string{
"email": strings.ToLower(nextUser.PrimaryEmail),
"familyName": nextUser.Name.FamilyName,
"givenName": nextUser.Name.GivenName,
"fullName": nextUser.Name.FullName,
},
if nextUser != nil {
people = append(people, extractData(*nextUser))
}

users = append(users, newPerson)
}

return users, nil
return people, nil
}

func (g *GoogleUsers) ApplyChangeSet(
Expand All @@ -111,6 +177,49 @@ func (g *GoogleUsers) ApplyChangeSet(
return results
}

func newUserForUpdate(person personnel_sync.Person) admin.User {
newName := admin.UserName{
GivenName: person.Attributes["givenName"],
FamilyName: person.Attributes["familyName"],
}

id := admin.UserExternalId{
Type: "organization",
Value: person.Attributes["id"],
}

location := admin.UserLocation{
Type: "desk",
Area: person.Attributes["area"],
BuildingId: person.Attributes["building"],
}

organization := admin.UserOrganization{
CostCenter: person.Attributes["costCenter"],
Department: person.Attributes["department"],
Title: person.Attributes["title"],
}

phone := admin.UserPhone{
Type: "work",
Value: person.Attributes["phone"],
}

relation := admin.UserRelation{
Type: "manager",
Value: person.Attributes["manager"],
}

return admin.User{
Name: &newName,
ExternalIds: []admin.UserExternalId{id},
Locations: []admin.UserLocation{location},
Organizations: []admin.UserOrganization{organization},
Phones: []admin.UserPhone{phone},
Relations: []admin.UserRelation{relation},
}
}

func (g *GoogleUsers) updateUser(
person personnel_sync.Person,
counter *uint64,
Expand All @@ -119,14 +228,7 @@ func (g *GoogleUsers) updateUser(

defer wg.Done()

newName := admin.UserName{
GivenName: person.Attributes["givenName"],
FamilyName: person.Attributes["familyName"],
}

newUser := admin.User{
Name: &newName,
}
newUser := newUserForUpdate(person)

email := person.Attributes["email"]

Expand Down
Loading

0 comments on commit 18a19e4

Please sign in to comment.