Skip to content

Commit

Permalink
Merge pull request #11 from cthit/gamma-integration
Browse files Browse the repository at this point in the history
Gamma integration
  • Loading branch information
molleer authored Jul 6, 2020
2 parents cb74a10 + 30d8ce8 commit 8f3048f
Show file tree
Hide file tree
Showing 10 changed files with 806 additions and 7 deletions.
44 changes: 44 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
version: "3"
services:
gamma-db:
image: postgres:10
environment:
POSTGRES_USER: user
POSTGRES_DB: postgres
POSTGRES_PASSWORD: password

gamma-frontend:
image: cthit/gamma-frontend:development
network_mode: host
ports:
- 3000:3000

gamma-backend:
image: cthit/gamma-backend:latest
environment:
# Default admin user name = admin
# Default admin password = password

DB_USER: user
DB_PASSWORD: password
DB_HOST: gamma-db
DB_PORT: 5432
DB_NAME: postgres

REDIS_HOST: redis
REDIS_PASSWORD: ""

SERVER_PORT: 8081
FRONTEND_REDIRECT_URI: http://localhost:3000/login
SUCCESSFUL_LOGIN: http://localhost:3000
CORS_ALLOWED_ORIGIN: http://localhost:3000
COOKIE_DOMAIN: localhost
PRODUCTION: "false"
IS_MOCKING_CLIENT: "true"
DEFAULT_REDIRECT_URI: http://localhost:3001/callback

ports:
- 8081:8081

redis:
image: redis:5.0
4 changes: 4 additions & 0 deletions example.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
servicekeyfile = "gapps.json"
adminaccount = "[email protected]"

[gamma.producer]
apiKey = "key"
url = "http://localhost:8080"

[ldap]
url = "ldap.mydomain.ex:636"
servername = "mydomain.ex"
Expand Down
58 changes: 53 additions & 5 deletions internal/app/cli/bgc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package cli

import (
"fmt"

"github.com/cthit/goldapps/internal/pkg/actions"
"github.com/cthit/goldapps/internal/pkg/duplicates"
"github.com/cthit/goldapps/internal/pkg/model"
)

const (
colorRed = "\033[31m"
colorGreen = "\033[32m"
)

func init() {
loadFlags()

Expand Down Expand Up @@ -109,6 +115,52 @@ func Run() {
}
}

func has(member string, members []string) bool {
for _, v := range members {
if model.CompareEmails(v, member) {
return true
}
}
return false
}

func printGroupDiff(before model.Group, after model.Group) {
fmt.Printf("\tUpdate: ")
if before.Email != after.Email {
fmt.Printf("\t%s -> %s\n", before.Email, after.Email)
} else {
fmt.Printf("\t\t%s\n", after.Email)
}
if before.Type != after.Email {
fmt.Printf("\t\t\t%s -> %s\n", before.Type, after.Type)
}

added := []string{}
deleted := []string{}

for _, member := range before.Members {
if !has(member, after.Members) {
deleted = append(deleted, member)
}
}

for _, member := range after.Members {
if !has(member, before.Members) {
added = append(added, member)
}
}

for _, del := range deleted {
fmt.Printf("\t\t\t- %s\n", del)
}

for _, add := range added {
fmt.Printf("\t\t\t+ %s\n", add)
}

fmt.Printf("\t\t\tAliases: %v -> %v\n", before.Aliases, after.Aliases)
}

func getGroupChanges(proposedChanges actions.GroupActions) actions.GroupActions {
if !flags.interactive && flags.noInteraction {
fmt.Printf(
Expand Down Expand Up @@ -137,11 +189,7 @@ func getGroupChanges(proposedChanges actions.GroupActions) actions.GroupActions
fmt.Printf("(Groups) Changes (%d):\n", len(proposedChanges.Updates))
if len(proposedChanges.Updates) > 0 {
for _, update := range proposedChanges.Updates {
fmt.Printf("\tUpdate:\n")
fmt.Printf("\t\tFrom:\n")
fmt.Printf("\t\t\t%v\n", update.Before)
fmt.Printf("\t\tTo:\n")
fmt.Printf("\t\t\t%v\n", update.After)
printGroupDiff(update.Before, update.After)
}
add := askBool(
fmt.Sprintf("(Groups) Do you want to commit those %d updates?", len(proposedChanges.Updates)),
Expand Down
16 changes: 14 additions & 2 deletions internal/app/cli/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package cli

import (
"fmt"
"regexp"

"github.com/cthit/goldapps/internal/pkg/model"
"github.com/cthit/goldapps/internal/pkg/services"
"github.com/cthit/goldapps/internal/pkg/services/admin"
"github.com/cthit/goldapps/internal/pkg/services/gamma"
"github.com/cthit/goldapps/internal/pkg/services/json"
"github.com/cthit/goldapps/internal/pkg/services/ldap"
"github.com/spf13/viper"
"regexp"
)

func getConsumer() services.UpdateService {
Expand Down Expand Up @@ -49,7 +51,7 @@ func getConsumer() services.UpdateService {
func getProvider() services.CollectionService {
var from string
if flags.interactive {
from = askString("which providers would you like to use, 'ldap', 'gapps' or '*.json'?", "ldap")
from = askString("which providers would you like to use, 'ldap', 'gapps', 'gamma' or '*.json'?", "ldap")
} else {
from = flags.from
}
Expand All @@ -69,6 +71,16 @@ func getProvider() services.CollectionService {
panic(err)
}
return provider
case "gamma":
provider, err := gamma.CreateGammaService(
viper.GetString("gamma.provider.apiKey"),
viper.GetString("gamma.provider.url"))
if err != nil {
fmt.Println("Failed to connect to Gamma")
panic(err)
}
return provider

default:
isJson, _ := regexp.MatchString(`.+\.json$`, from)
if isJson {
Expand Down
96 changes: 96 additions & 0 deletions internal/pkg/services/gamma/gammaRequests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package gamma

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)

//Executes a generic get request to Gamma
func gammaReq(s *GammaService, endpoint string, response interface{}) error {
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", s.gammaUrl, endpoint), nil)
if err != nil {
log.Println(err)
return err
}

req.Header.Set("Authorization", fmt.Sprintf("pre-shared %s", s.apiKey))

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return err
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return err
}

err = json.Unmarshal(body, &response)
if err != nil {
log.Println(err)
return err
}

return nil
}

//Fetches all the fkit-groups from Gamma
func getGammaGroups(s *GammaService) ([]FKITGroup, error) {
var groups struct {
Groups []FKITGroup `json:"groups"`
}

err := gammaReq(s, "/api/groups", &groups)
if err != nil {
log.Println(err)
return nil, err
}

return groups.Groups, nil
}

//Fetches all super groups from Gamma
func getSuperGroups(s *GammaService) ([]FKITSuperGroup, error) {
var superGroups []FKITSuperGroup

err := gammaReq(s, "/api/superGroups", &superGroups)
if err != nil {
log.Println(err)
return nil, err
}

return superGroups, nil
}

//Fetches all posts which has a mail prefix
func getMailPosts(s *GammaService) ([]Post, error) {
posts := []Post{}
err := gammaReq(s, "/api/groups/posts", &posts)
if err != nil {
return nil, err
}

mailPosts := []Post{}
for _, post := range posts {
if post.EmailPrefix != "" {
mailPosts = append(mailPosts, post)
}
}

return mailPosts, nil
}

//Fetches all active groups
func getActiveGroups(s *GammaService) ([]FKITGroup, error) {
groups := struct {
GetFKITGroupResponse []FKITGroup `json:"getFKITGroupResponse"`
}{}
err := gammaReq(s, "/api/groups/active", &groups)
return groups.GetFKITGroupResponse, err
}
Loading

0 comments on commit 8f3048f

Please sign in to comment.