|
| 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 | +} |
0 commit comments