|
| 1 | +package mongodbatlas |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | +) |
| 8 | + |
| 9 | +const accessListAPIKeysPath = "orgs/%s/apiKeys/%s/accesslist" |
| 10 | + |
| 11 | +// AccessListAPIKeysService is an interface for interfacing with the AccessList API Keys |
| 12 | +// endpoints of the MongoDB Atlas API. |
| 13 | +// |
| 14 | +// See more: https://docs.atlas.mongodb.com/reference/api/apiKeys#organization-api-key-access-list-endpoints |
| 15 | +type AccessListAPIKeysService interface { |
| 16 | + List(context.Context, string, string, *ListOptions) (*AccessListAPIKeys, *Response, error) |
| 17 | + Get(context.Context, string, string, string) (*AccessListAPIKey, *Response, error) |
| 18 | + Create(context.Context, string, string, []*AccessListAPIKeysReq) (*AccessListAPIKeys, *Response, error) |
| 19 | + Delete(context.Context, string, string, string) (*Response, error) |
| 20 | +} |
| 21 | + |
| 22 | +// AccessListAPIKeysServiceOp handles communication with the AccessList API keys related methods of the |
| 23 | +// MongoDB Atlas API |
| 24 | +type AccessListAPIKeysServiceOp service |
| 25 | + |
| 26 | +var _ AccessListAPIKeysService = &AccessListAPIKeysServiceOp{} |
| 27 | + |
| 28 | +// AccessListAPIKey represents a AccessList API key. |
| 29 | +type AccessListAPIKey struct { |
| 30 | + CidrBlock string `json:"cidrBlock,omitempty"` // CIDR-notated range of permitted IP addresses. |
| 31 | + Count int `json:"count,omitempty"` // Total number of requests that have originated from this IP address. |
| 32 | + Created string `json:"created,omitempty"` // Date this IP address was added to the access list. |
| 33 | + IPAddress string `json:"ipAddress,omitempty"` // IP address in the API access list. |
| 34 | + LastUsed string `json:"lastUsed,omitempty"` // Timestamp in ISO 8601 date and time format in UTC when the most recent request that originated from this IP address. This parameter only appears if at least one request has originated from this IP address, and is only updated when a permitted resource is accessed. |
| 35 | + LastUsedAddress string `json:"lastUsedAddress,omitempty"` // IP address from which the last call to the API was issued. This field only appears if at least one request has originated from this IP address. |
| 36 | + Links []*Link `json:"links,omitempty"` // An array of documents, representing a link to one or more sub-resources and/or related resources such as list pagination. See Linking for more information.} |
| 37 | +} |
| 38 | + |
| 39 | +// AccessListAPIKeys represents all AccessList API keys. |
| 40 | +type AccessListAPIKeys struct { |
| 41 | + Results []*AccessListAPIKey `json:"results,omitempty"` // Includes one AccessListAPIKey object for each item detailed in the results array section. |
| 42 | + Links []*Link `json:"links,omitempty"` // One or more links to sub-resources and/or related resources. |
| 43 | + TotalCount int `json:"totalCount,omitempty"` // Count of the total number of items in the result set. It may be greater than the number of objects in the results array if the entire result set is paginated. |
| 44 | +} |
| 45 | + |
| 46 | +// AccessListAPIKeysReq represents the request to the mehtod create |
| 47 | +type AccessListAPIKeysReq struct { |
| 48 | + IPAddress string `json:"ipAddress,omitempty"` // IP address to be added to the access list for the API key. |
| 49 | + CidrBlock string `json:"cidrBlock,omitempty"` // CIDR-notation block of IP addresses to be added to the access list for the API key. |
| 50 | +} |
| 51 | + |
| 52 | +// List gets all AccessList API keys. |
| 53 | +// |
| 54 | +// See more: https://docs.atlas.mongodb.com/reference/api/api-access-list/get-all-api-access-entries/ |
| 55 | +func (s *AccessListAPIKeysServiceOp) List(ctx context.Context, orgID, apiKeyID string, listOptions *ListOptions) (*AccessListAPIKeys, *Response, error) { |
| 56 | + if orgID == "" { |
| 57 | + return nil, nil, NewArgError("orgID", "must be set") |
| 58 | + } |
| 59 | + if apiKeyID == "" { |
| 60 | + return nil, nil, NewArgError("apiKeyID", "must be set") |
| 61 | + } |
| 62 | + |
| 63 | + path := fmt.Sprintf(accessListAPIKeysPath, orgID, apiKeyID) |
| 64 | + path, err := setListOptions(path, listOptions) |
| 65 | + if err != nil { |
| 66 | + return nil, nil, err |
| 67 | + } |
| 68 | + |
| 69 | + req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil) |
| 70 | + if err != nil { |
| 71 | + return nil, nil, err |
| 72 | + } |
| 73 | + |
| 74 | + root := new(AccessListAPIKeys) |
| 75 | + resp, err := s.Client.Do(ctx, req, root) |
| 76 | + if err != nil { |
| 77 | + return nil, resp, err |
| 78 | + } |
| 79 | + |
| 80 | + if l := root.Links; l != nil { |
| 81 | + resp.Links = l |
| 82 | + } |
| 83 | + |
| 84 | + return root, resp, nil |
| 85 | +} |
| 86 | + |
| 87 | +// Get retrieve information on a single API Key access list entry using the unique identifier for the API Key and desired permitted address. |
| 88 | +// |
| 89 | +// See more: https://docs.atlas.mongodb.com/reference/api/api-access-list/get-one-api-access-entry/ |
| 90 | +func (s *AccessListAPIKeysServiceOp) Get(ctx context.Context, orgID, apiKeyID, ipAddress string) (*AccessListAPIKey, *Response, error) { |
| 91 | + if orgID == "" { |
| 92 | + return nil, nil, NewArgError("orgID", "must be set") |
| 93 | + } |
| 94 | + if apiKeyID == "" { |
| 95 | + return nil, nil, NewArgError("apiKeyID", "must be set") |
| 96 | + } |
| 97 | + if ipAddress == "" { |
| 98 | + return nil, nil, NewArgError("ipAddress", "must be set") |
| 99 | + } |
| 100 | + |
| 101 | + path := fmt.Sprintf(accessListAPIKeysPath+"/%s", orgID, apiKeyID, ipAddress) |
| 102 | + |
| 103 | + req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil) |
| 104 | + if err != nil { |
| 105 | + return nil, nil, err |
| 106 | + } |
| 107 | + |
| 108 | + root := new(AccessListAPIKey) |
| 109 | + resp, err := s.Client.Do(ctx, req, root) |
| 110 | + if err != nil { |
| 111 | + return nil, resp, err |
| 112 | + } |
| 113 | + |
| 114 | + return root, resp, err |
| 115 | +} |
| 116 | + |
| 117 | +// Create one or more new access list entries for the specified API Key. |
| 118 | +// |
| 119 | +// See more: https://docs.atlas.mongodb.com/reference/api/api-access-list/create-api-access-entries/ |
| 120 | +func (s *AccessListAPIKeysServiceOp) Create(ctx context.Context, orgID, apiKeyID string, createRequest []*AccessListAPIKeysReq) (*AccessListAPIKeys, *Response, error) { |
| 121 | + if orgID == "" { |
| 122 | + return nil, nil, NewArgError("orgID", "must be set") |
| 123 | + } |
| 124 | + if apiKeyID == "" { |
| 125 | + return nil, nil, NewArgError("apiKeyID", "must be set") |
| 126 | + } |
| 127 | + if createRequest == nil { |
| 128 | + return nil, nil, NewArgError("createRequest", "cannot be nil") |
| 129 | + } |
| 130 | + |
| 131 | + path := fmt.Sprintf(accessListAPIKeysPath, orgID, apiKeyID) |
| 132 | + |
| 133 | + req, err := s.Client.NewRequest(ctx, http.MethodPost, path, createRequest) |
| 134 | + if err != nil { |
| 135 | + return nil, nil, err |
| 136 | + } |
| 137 | + |
| 138 | + root := new(AccessListAPIKeys) |
| 139 | + resp, err := s.Client.Do(ctx, req, root) |
| 140 | + if err != nil { |
| 141 | + return nil, resp, err |
| 142 | + } |
| 143 | + |
| 144 | + return root, resp, err |
| 145 | +} |
| 146 | + |
| 147 | +// Delete deletes the AccessList API keys. |
| 148 | +// |
| 149 | +// See more: https://docs.atlas.mongodb.com/reference/api/api-access-list/delete-one-api-access-entry/ |
| 150 | +func (s *AccessListAPIKeysServiceOp) Delete(ctx context.Context, orgID, apiKeyID, ipAddress string) (*Response, error) { |
| 151 | + if orgID == "" { |
| 152 | + return nil, NewArgError("orgID", "must be set") |
| 153 | + } |
| 154 | + if apiKeyID == "" { |
| 155 | + return nil, NewArgError("apiKeyID", "must be set") |
| 156 | + } |
| 157 | + if ipAddress == "" { |
| 158 | + return nil, NewArgError("ipAddress", "must be set") |
| 159 | + } |
| 160 | + |
| 161 | + path := fmt.Sprintf(accessListAPIKeysPath+"/%s", orgID, apiKeyID, ipAddress) |
| 162 | + |
| 163 | + req, err := s.Client.NewRequest(ctx, http.MethodDelete, path, nil) |
| 164 | + if err != nil { |
| 165 | + return nil, err |
| 166 | + } |
| 167 | + resp, err := s.Client.Do(ctx, req, nil) |
| 168 | + |
| 169 | + return resp, err |
| 170 | +} |
0 commit comments