github.1485827954.workers.dev/newrelic/newrelic-client-go@v1.1.0/pkg/apiaccess/keys.go (about) 1 package apiaccess 2 3 import ( 4 "context" 5 "errors" 6 7 "github.com/newrelic/newrelic-client-go/internal/http" 8 ) 9 10 type APIKey struct { 11 APIAccessKey 12 13 AccountID *int `json:"accountId,omitempty"` 14 IngestType APIAccessIngestKeyType `json:"ingestType,omitempty"` 15 UserID *int `json:"userId,omitempty"` 16 } 17 18 // 19 // Additional Interface methods 20 // 21 func (x *APIAccessIngestKeyError) GetError() error { 22 return errors.New(x.Message) 23 } 24 25 func (x *APIAccessUserKeyError) GetError() error { 26 return errors.New(x.Message) 27 } 28 29 // CreateAPIAccessKeys create keys. You can create keys for multiple accounts at once. 30 func (a *APIAccess) CreateAPIAccessKeys(keys APIAccessCreateInput) ([]APIKey, error) { 31 return a.CreateAPIAccessKeysWithContext(context.Background(), keys) 32 } 33 34 // CreateAPIAccessKeysWithContext create keys. You can create keys for multiple accounts at once. 35 func (a *APIAccess) CreateAPIAccessKeysWithContext(ctx context.Context, keys APIAccessCreateInput) ([]APIKey, error) { 36 vars := map[string]interface{}{ 37 "keys": keys, 38 } 39 40 resp := apiAccessKeyCreateResponse{} 41 42 if err := a.client.NerdGraphQueryWithContext(ctx, apiAccessKeyCreateKeys, vars, &resp); err != nil { 43 return nil, err 44 } 45 46 if len(resp.APIAccessCreateKeys.Errors) > 0 { 47 return nil, errors.New(formatAPIAccessKeyErrors(resp.APIAccessCreateKeys.Errors)) 48 } 49 50 return resp.APIAccessCreateKeys.CreatedKeys, nil 51 } 52 53 // GetAPIAccessKey returns a single API access key. 54 func (a *APIAccess) GetAPIAccessKey(keyID string, keyType APIAccessKeyType) (*APIKey, error) { 55 return a.GetAPIAccessKeyWithContext(context.Background(), keyID, keyType) 56 } 57 58 // GetAPIAccessKeyWithContext returns a single API access key. 59 func (a *APIAccess) GetAPIAccessKeyWithContext(ctx context.Context, keyID string, keyType APIAccessKeyType) (*APIKey, error) { 60 vars := map[string]interface{}{ 61 "id": keyID, 62 "keyType": keyType, 63 } 64 65 resp := apiAccessKeyGetResponse{} 66 67 if err := a.client.NerdGraphQueryWithContext(ctx, apiAccessKeyGetKey, vars, &resp); err != nil { 68 return nil, err 69 } 70 71 if resp.Errors != nil { 72 return nil, errors.New(resp.Error()) 73 } 74 75 return &resp.Actor.APIAccess.Key, nil 76 } 77 78 // SearchAPIAccessKeys returns the relevant keys based on search criteria. Returns keys are scoped to the current user. 79 func (a *APIAccess) SearchAPIAccessKeys(params APIAccessKeySearchQuery) ([]APIKey, error) { 80 return a.SearchAPIAccessKeysWithContext(context.Background(), params) 81 } 82 83 // SearchAPIAccessKeysWithContext returns the relevant keys based on search criteria. Returns keys are scoped to the current user. 84 func (a *APIAccess) SearchAPIAccessKeysWithContext(ctx context.Context, params APIAccessKeySearchQuery) ([]APIKey, error) { 85 vars := map[string]interface{}{ 86 "query": params, 87 } 88 89 resp := apiAccessKeySearchResponse{} 90 91 if err := a.client.NerdGraphQueryWithContext(ctx, apiAccessKeySearch, vars, &resp); err != nil { 92 return nil, err 93 } 94 95 if resp.Errors != nil { 96 return nil, errors.New(resp.Error()) 97 } 98 99 return resp.Actor.APIAccess.KeySearch.Keys, nil 100 } 101 102 // UpdateAPIAccessKeys updates keys. You can update keys for multiple accounts at once. 103 func (a *APIAccess) UpdateAPIAccessKeys(keys APIAccessUpdateInput) ([]APIKey, error) { 104 return a.UpdateAPIAccessKeysWithContext(context.Background(), keys) 105 } 106 107 // UpdateAPIAccessKeysWithContext updates keys. You can update keys for multiple accounts at once. 108 func (a *APIAccess) UpdateAPIAccessKeysWithContext(ctx context.Context, keys APIAccessUpdateInput) ([]APIKey, error) { 109 vars := map[string]interface{}{ 110 "keys": keys, 111 } 112 113 resp := apiAccessKeyUpdateResponse{} 114 115 if err := a.client.NerdGraphQueryWithContext(ctx, apiAccessKeyUpdateKeys, vars, &resp); err != nil { 116 return nil, err 117 } 118 119 if len(resp.APIAccessUpdateKeys.Errors) > 0 { 120 return nil, errors.New(formatAPIAccessKeyErrors(resp.APIAccessUpdateKeys.Errors)) 121 } 122 123 return resp.APIAccessUpdateKeys.UpdatedKeys, nil 124 } 125 126 // DeleteAPIAccessKey deletes one or more keys. 127 func (a *APIAccess) DeleteAPIAccessKey(keys APIAccessDeleteInput) ([]APIAccessDeletedKey, error) { 128 return a.DeleteAPIAccessKeyWithContext(context.Background(), keys) 129 } 130 131 // DeleteAPIAccessKeyWithContext deletes one or more keys. 132 func (a *APIAccess) DeleteAPIAccessKeyWithContext(ctx context.Context, keys APIAccessDeleteInput) ([]APIAccessDeletedKey, error) { 133 vars := map[string]interface{}{ 134 "keys": keys, 135 } 136 137 resp := apiAccessKeyDeleteResponse{} 138 139 if err := a.client.NerdGraphQueryWithContext(ctx, apiAccessKeyDeleteKeys, vars, &resp); err != nil { 140 return nil, err 141 } 142 143 if len(resp.APIAccessDeleteKeys.Errors) > 0 { 144 return nil, errors.New(formatAPIAccessKeyErrors(resp.APIAccessDeleteKeys.Errors)) 145 } 146 147 return resp.APIAccessDeleteKeys.DeletedKeys, nil 148 } 149 150 func formatAPIAccessKeyErrors(errs []APIAccessKeyErrorInterface) string { 151 errorString := "" 152 for _, e := range errs { 153 errorString += e.GetError().Error() + "\n" 154 } 155 return errorString 156 } 157 158 // apiAccessKeyCreateResponse represents the JSON response returned from creating key(s). 159 type apiAccessKeyCreateResponse struct { 160 APIAccessCreateKeys struct { 161 CreatedKeys []APIKey `json:"createdKeys"` 162 Errors []APIAccessKeyErrorInterface `json:"errors"` 163 } `json:"apiAccessCreateKeys"` 164 } 165 166 // apiAccessKeyUpdateResponse represents the JSON response returned from updating key(s). 167 type apiAccessKeyUpdateResponse struct { 168 APIAccessUpdateKeys struct { 169 UpdatedKeys []APIKey `json:"updatedKeys"` 170 Errors []APIAccessKeyErrorInterface `json:"errors"` 171 } `json:"apiAccessUpdateKeys"` 172 } 173 174 // apiAccessKeyGetResponse represents the JSON response returned from getting an access key. 175 type apiAccessKeyGetResponse struct { 176 Actor struct { 177 APIAccess struct { 178 Key APIKey `json:"key,omitempty"` 179 } `json:"apiAccess"` 180 } `json:"actor"` 181 http.GraphQLErrorResponse 182 } 183 184 type apiAccessKeySearchResponse struct { 185 Actor struct { 186 APIAccess struct { 187 KeySearch struct { 188 Keys []APIKey `json:"keys"` 189 } `json:"keySearch,omitempty"` 190 } `json:"apiAccess"` 191 } `json:"actor"` 192 http.GraphQLErrorResponse 193 } 194 195 // apiAccessKeyDeleteResponse represents the JSON response returned from creating key(s). 196 type apiAccessKeyDeleteResponse struct { 197 APIAccessDeleteKeys APIAccessDeleteKeyResponse `json:"apiAccessDeleteKeys"` 198 } 199 200 const ( 201 graphqlAPIAccessKeyBaseFields = ` 202 id 203 key 204 name 205 notes 206 type 207 ... on ApiAccessIngestKey { 208 id 209 name 210 accountId 211 ingestType 212 key 213 notes 214 type 215 } 216 ... on ApiAccessUserKey { 217 id 218 name 219 accountId 220 key 221 notes 222 type 223 userId 224 } 225 ... on ApiAccessKey { 226 id 227 name 228 key 229 notes 230 type 231 }` 232 233 graphqlAPIAccessCreateKeyFields = `createdKeys {` + graphqlAPIAccessKeyBaseFields + `}` 234 235 graphqlAPIAccessUpdatedKeyFields = `updatedKeys {` + graphqlAPIAccessKeyBaseFields + `}` 236 237 graphqlAPIAccessKeyErrorFields = `errors { 238 message 239 type 240 ... on ApiAccessIngestKeyError { 241 id 242 ingestErrorType: errorType 243 accountId 244 ingestType 245 message 246 type 247 } 248 ... on ApiAccessKeyError { 249 message 250 type 251 } 252 ... on ApiAccessUserKeyError { 253 id 254 accountId 255 userErrorType: errorType 256 message 257 type 258 userId 259 } 260 } 261 ` 262 263 apiAccessKeyCreateKeys = `mutation($keys: ApiAccessCreateInput!) { 264 apiAccessCreateKeys(keys: $keys) {` + graphqlAPIAccessCreateKeyFields + graphqlAPIAccessKeyErrorFields + ` 265 }}` 266 267 apiAccessKeyGetKey = `query($id: ID!, $keyType: ApiAccessKeyType!) { 268 actor { 269 apiAccess { 270 key(id: $id, keyType: $keyType) {` + graphqlAPIAccessKeyBaseFields + `}}}}` 271 272 apiAccessKeySearch = `query($query: ApiAccessKeySearchQuery!) { 273 actor { 274 apiAccess { 275 keySearch(query: $query) { 276 keys {` + graphqlAPIAccessKeyBaseFields + `} 277 }}}}` 278 279 apiAccessKeyUpdateKeys = `mutation($keys: ApiAccessUpdateInput!) { 280 apiAccessUpdateKeys(keys: $keys) {` + graphqlAPIAccessUpdatedKeyFields + graphqlAPIAccessKeyErrorFields + ` 281 }}` 282 283 apiAccessKeyDeleteKeys = `mutation($keys: ApiAccessDeleteInput!) { 284 apiAccessDeleteKeys(keys: $keys) { 285 deletedKeys { 286 id 287 }` + graphqlAPIAccessKeyErrorFields + `}}` 288 )