Skip to content
This repository was archived by the owner on May 24, 2024. It is now read-only.

Commit af7c858

Browse files
committed
Merge branch 'main' of github.com:LF-Engineering/dev-analytics-libraries into add-common-util
2 parents e21c75d + e1de595 commit af7c858

File tree

12 files changed

+791
-26
lines changed

12 files changed

+791
-26
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ lint: fmt
44
golint -set_exit_status $(PKG_LIST)
55

66
fmt:
7-
./scripts/for_each_go_file.sh gofmt -s -w
7+
./scripts/for_each_go_file.sh 'gofmt -s -w'
88

99
test:
1010
go test -v $(PKG_LIST)

affiliation/dto.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package affiliation
2+
3+
import "time"
4+
5+
// Identity ...
6+
type Identity struct {
7+
ID string `json:"id"`
8+
LastModified time.Time `json:"last_modified"`
9+
Name string `json:"name"`
10+
Source string `json:"source"`
11+
Username string `json:"username"`
12+
Email string `json:"email"`
13+
UUID string `json:"uuid"`
14+
}
15+
16+
// AffiliationsResponse ...
17+
type AffiliationsResponse struct {
18+
Code string `json:"Code"`
19+
Message string `json:"Message"`
20+
Enrollments interface{} `json:"enrollments"`
21+
Identities interface{} `json:"identities"`
22+
LastModified time.Time `json:"last_modified"`
23+
Profile struct {
24+
UUID string `json:"uuid"`
25+
} `json:"profile"`
26+
UUID string `json:"uuid"`
27+
}

affiliation/identity.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package affiliation
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
"net/url"
8+
"strings"
9+
10+
"github.com/LF-Engineering/dev-analytics-libraries/auth0"
11+
"github.com/LF-Engineering/dev-analytics-libraries/elastic"
12+
"github.com/LF-Engineering/dev-analytics-libraries/http"
13+
)
14+
15+
// Affiliations interface
16+
type Affiliations interface {
17+
AddIdentity(identity *Identity) bool
18+
}
19+
20+
// Affiliation struct
21+
type Affiliation struct {
22+
AffBaseURL string
23+
ProjectSlug string
24+
ESCacheURL string
25+
ESCacheUsername string
26+
ESCachePassword string
27+
AuthGrantType string
28+
AuthClientID string
29+
AuthClientSecret string
30+
AuthAudience string
31+
AuthURL string
32+
Environment string
33+
httpClient *http.ClientProvider
34+
esClient *elastic.ClientProvider
35+
auth0Client *auth0.ClientProvider
36+
}
37+
38+
// NewAffiliationsClient consumes
39+
// affBaseURL, projectSlug, esCacheUrl, esCacheUsername, esCachePassword, esCacheIndex, env, authGrantType, authClientID, authClientSecret, authAudience, authURL
40+
func NewAffiliationsClient(affBaseURL, projectSlug, esCacheURL, esCacheUsername, esCachePassword, env, authGrantType, authClientID, authClientSecret, authAudience, authURL string) (*Affiliation, error) {
41+
aff := &Affiliation{
42+
AffBaseURL: affBaseURL,
43+
ProjectSlug: projectSlug,
44+
ESCacheURL: esCacheURL,
45+
ESCacheUsername: esCacheUsername,
46+
ESCachePassword: esCachePassword,
47+
AuthGrantType: authGrantType,
48+
AuthClientID: authClientID,
49+
AuthClientSecret: authClientSecret,
50+
AuthAudience: authAudience,
51+
AuthURL: authURL,
52+
Environment: env,
53+
}
54+
55+
httpClientProvider, esClientProvider, auth0ClientProvider, err := buildServices(aff)
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
aff.esClient = esClientProvider
61+
aff.httpClient = httpClientProvider
62+
aff.auth0Client = auth0ClientProvider
63+
64+
return aff, nil
65+
}
66+
67+
// AddIdentity ...
68+
func (a *Affiliation) AddIdentity(identity *Identity) bool {
69+
if identity == nil {
70+
log.Println("Repository: AddIdentity: Identity is nil")
71+
return false
72+
}
73+
token, err := a.auth0Client.ValidateToken(a.Environment)
74+
if err != nil {
75+
log.Println(err)
76+
}
77+
headers := make(map[string]string, 0)
78+
headers["Content-type"] = "application/json"
79+
headers["Authorization"] = fmt.Sprintf("%s %s", "Bearer", token)
80+
81+
queryParams := make(map[string]string, 0)
82+
queryParams["name"] = identity.Name
83+
queryParams["username"] = identity.Username
84+
queryParams["email"] = identity.Email
85+
queryParams["uuid"] = identity.UUID
86+
87+
endpoint := a.AffBaseURL + "/affiliation/" + url.PathEscape(a.ProjectSlug)+ "/add_identity/" + url.PathEscape(identity.Source)
88+
_, res, err := a.httpClient.Request(strings.TrimSpace(endpoint), "POST", headers, nil, queryParams)
89+
if err != nil {
90+
log.Println("Repository: AddIdentity: Could not insert the identity: ", err)
91+
return false
92+
}
93+
var errMsg AffiliationsResponse
94+
err = json.Unmarshal(res, &errMsg)
95+
if err != nil || errMsg.Message != "" {
96+
log.Println("Repository: AddIdentity: failed to add identity: ", err)
97+
return false
98+
}
99+
return true
100+
}
101+
102+
func buildServices(a *Affiliation) (httpClientProvider *http.ClientProvider, esClientProvider *elastic.ClientProvider, auth0ClientProvider *auth0.ClientProvider, err error) {
103+
esClientProvider, err = elastic.NewClientProvider(&elastic.Params{
104+
URL: a.ESCacheURL,
105+
Username: a.ESCacheUsername,
106+
Password: a.ESCachePassword,
107+
})
108+
if err != nil {
109+
return
110+
}
111+
112+
httpClientProvider = http.NewClientProvider(60)
113+
114+
auth0ClientProvider, err = auth0.NewAuth0Client(a.ESCacheURL, a.ESCacheUsername, a.ESCachePassword, a.Environment, a.AuthGrantType, a.AuthClientID, a.AuthClientSecret, a.AuthAudience, a.AuthURL)
115+
if err != nil {
116+
return
117+
}
118+
119+
return
120+
}

auth0/dto.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package auth0
2+
3+
// AuthToken Struct
4+
type AuthToken struct {
5+
Name string `json:"name"`
6+
Token string `json:"token"`
7+
}
8+
9+
// Resp struct
10+
type Resp struct {
11+
AccessToken string `json:"access_token"`
12+
Scope string `json:"scope"`
13+
ExpiresIn int `json:"expires_in"`
14+
TokenType string `json:"token_type"`
15+
}
16+
17+
// ESTokenSchema for AuthToken
18+
type ESTokenSchema struct {
19+
Took int `json:"took"`
20+
TimedOut bool `json:"timed_out"`
21+
Shards struct {
22+
Total int `json:"total"`
23+
Successful int `json:"successful"`
24+
Skipped int `json:"skipped"`
25+
Failed int `json:"failed"`
26+
} `json:"_shards"`
27+
Hits struct {
28+
Total struct {
29+
Value int `json:"value"`
30+
Relation string `json:"relation"`
31+
} `json:"total"`
32+
MaxScore float64 `json:"max_score"`
33+
Hits []struct {
34+
Index string `json:"_index"`
35+
Type string `json:"_type"`
36+
ID string `json:"_id"`
37+
Score float64 `json:"_score"`
38+
Source struct {
39+
Name string `json:"name"`
40+
Token string `json:"token"`
41+
} `json:"_source"`
42+
} `json:"hits"`
43+
} `json:"hits"`
44+
}

0 commit comments

Comments
 (0)