@@ -84,23 +84,23 @@ func GetAccessToken(secretsLister v1.SecretLister) (string, error) {
84
84
// 1. custom ORGANIZATION_ID is awailable as telemetry annotation on console-operator config or in telemetry-config configmap
85
85
// 2. cached ORGANIZATION_ID is available on the operator controller instance
86
86
// else fetch the ORGANIZATION_ID from OCM
87
- func GetOrganizationID (telemetryConfig map [string ]string , cachedOrganizationID , clusterID , accessToken string ) (string , bool ) {
87
+ func GetOrganizationMeta (telemetryConfig map [string ]string , cachedOrganizationID , cachedAccountEmail , clusterID , accessToken string ) (string , string , bool ) {
88
88
customOrganizationID , isCustomOrgIDSet := telemetryConfig ["ORGANIZATION_ID" ]
89
89
if isCustomOrgIDSet {
90
90
klog .V (4 ).Infoln ("telemetry config: using custom organization ID" )
91
- return customOrganizationID , false
91
+ return customOrganizationID , "" , false
92
92
}
93
93
94
- if cachedOrganizationID != "" {
95
- klog .V (4 ).Infoln ("telemetry config: using cached organization ID " )
96
- return cachedOrganizationID , false
94
+ if cachedOrganizationID != "" && cachedAccountEmail != "" {
95
+ klog .V (4 ).Infoln ("telemetry config: using cached organization metadata " )
96
+ return cachedOrganizationID , cachedAccountEmail , false
97
97
}
98
98
99
- fetchedOrganizationID , err := FetchOrganizationID (clusterID , accessToken )
99
+ fetchedOCMRespose , err := FetchSubscription (clusterID , accessToken )
100
100
if err != nil {
101
101
klog .Errorf ("telemetry config error: %s" , err )
102
102
}
103
- return fetchedOrganizationID , true
103
+ return fetchedOCMRespose . Organization . ExternalId , fetchedOCMRespose . Creator . Email , true
104
104
}
105
105
106
106
// Needed to create our own types for OCM Subscriptions since their types and client are useless
@@ -111,23 +111,28 @@ type OCMAPIResponse struct {
111
111
}
112
112
type Subscription struct {
113
113
Organization Organization `json:"organization,omitempty"`
114
+ Creator Creator `json:"creator,omitempty"`
115
+ }
116
+
117
+ type Creator struct {
118
+ Email string `json:"email,omitempty"`
114
119
}
115
120
116
121
type Organization struct {
117
122
ExternalId string `json:"external_id,omitempty"`
118
123
}
119
124
120
- // FetchOrganizationID fetches the organization ID using the cluster ID and access token
121
- func FetchOrganizationID (clusterID , accessToken string ) (string , error ) {
125
+ // FetchOrganizationMeta fetches the organization ID and Accout email using the cluster ID and access token
126
+ func FetchSubscription (clusterID , accessToken string ) (* Subscription , error ) {
122
127
klog .V (4 ).Infoln ("telemetry config: fetching organization ID" )
123
128
u , err := buildURL (clusterID )
124
129
if err != nil {
125
- return "" , err // more contextual error handling can be added here if needed
130
+ return nil , err // more contextual error handling can be added here if needed
126
131
}
127
132
128
133
req , err := createRequest (u , clusterID , accessToken )
129
134
if err != nil {
130
- return "" , err
135
+ return nil , err
131
136
}
132
137
133
138
client := & http.Client {
@@ -138,29 +143,31 @@ func FetchOrganizationID(clusterID, accessToken string) (string, error) {
138
143
}
139
144
resp , err := client .Do (req )
140
145
if err != nil {
141
- return "" , fmt .Errorf ("failed to GET (%s): %v" , u .String (), err )
146
+ return nil , fmt .Errorf ("failed to GET (%s): %v" , u .String (), err )
142
147
}
143
148
defer resp .Body .Close ()
144
149
145
150
if resp .StatusCode != http .StatusOK {
146
- return "" , fmt .Errorf ("HTTP request failed with status '%s'" , resp .Status )
151
+ return nil , fmt .Errorf ("HTTP request failed with status '%s'" , resp .Status )
147
152
}
148
153
149
154
body , err := io .ReadAll (resp .Body )
150
155
if err != nil {
151
- return "" , fmt .Errorf ("failed to read response body: %v" , err )
156
+ return nil , fmt .Errorf ("failed to read response body: %v" , err )
152
157
}
153
158
154
159
var ocmResponse OCMAPIResponse
155
160
if err = json .Unmarshal (body , & ocmResponse ); err != nil {
156
- return "" , fmt .Errorf ("error decoding JSON: %v" , err )
161
+ return nil , fmt .Errorf ("error decoding JSON: %v" , err )
157
162
}
163
+ klog .Infof ("---> data: %#v\n " , string (body ))
164
+ klog .Infof ("---> ocmResponse: %#v\n " , ocmResponse )
158
165
159
166
if len (ocmResponse .Items ) == 0 {
160
- return "" , fmt .Errorf ("empty OCM response" )
167
+ return nil , fmt .Errorf ("empty OCM response" )
161
168
}
162
169
163
- return ocmResponse .Items [0 ]. Organization . ExternalId , nil
170
+ return & ocmResponse .Items [0 ], nil
164
171
}
165
172
166
173
// buildURL constructs the URL for the API request
@@ -172,6 +179,7 @@ func buildURL(clusterID string) (*url.URL, error) {
172
179
}
173
180
q := u .Query ()
174
181
q .Add ("fetchOrganization" , "true" )
182
+ q .Add ("fetchAccounts" , "true" )
175
183
q .Add ("search" , fmt .Sprintf ("external_cluster_id='%s'" , clusterID ))
176
184
u .RawQuery = q .Encode ()
177
185
return u , nil
0 commit comments