Class: Cartography::Client
- Inherits:
-
Object
- Object
- Cartography::Client
- Defined in:
- lib/cartography/client.rb
Overview
Client class is the base method for making calls to the API. Methods either return plain objects or class instances.
Defined Under Namespace
Classes: InvalidClusterID, ResponseError
Constant Summary collapse
- RETRY_ERRORS =
[ HTTP::ConnectionError, HTTP::TimeoutError ]
Instance Attribute Summary collapse
-
#api_key ⇒ String
readonly
The API key used for authentication.
-
#dynamic ⇒ Object
readonly
Returns the value of attribute dynamic.
-
#endpoint ⇒ String
readonly
The API endpoint.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#retries ⇒ Object
readonly
Returns the value of attribute retries.
-
#the ⇒ Integer
readonly
number of retries for the request.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
-
#whether ⇒ Bool
readonly
to return dynamic data.
Instance Method Summary collapse
- #get(path, params: {}) ⇒ Object
- #get_body(path, params: {}) ⇒ Object
-
#get_cluster(type:, namespace:) ⇒ Array<Cluster>
Query the API for a specific cluster.
-
#get_cluster_acceptance_config(type:, namespace:, format: "json") ⇒ String
Return Goss configuration for a cluster.
-
#get_cluster_site(type:, namespace:, site:) ⇒ Cluster
Query the API for a specific cluster site by type, namespace and site.
-
#get_cluster_site_acceptance_config(type:, namespace:, site:, format: "json") ⇒ String
Return Goss configuration for a cluster site.
-
#get_cluster_site_server_group(type:, namespace:, site:, server_group:) ⇒ ServerGroup
Query the API for a specific server group in a specific cluster site by type, namespace and site.
-
#get_cluster_site_server_groups(type:, namespace:, site:) ⇒ Array<ServerGroup>
Query the API for all server groups in a specific cluster site by type, namespace and site.
-
#get_clusters ⇒ Array<Cluster>
Query the API for all clusters.
-
#get_clusters_by_type(type:) ⇒ Array<Cluster>
Query the API for clusters by type.
-
#get_custom_cname(cname_id:) ⇒ Class<CustomCnamey>
Query the API for a custom CNAME.
-
#get_custom_cname_acceptance_config(cname_id:, format: "json") ⇒ String
Return Goss configuration.
-
#get_custom_cnames ⇒ Array<CustomCname>
Returns all custom CNAMEs.
-
#get_custom_cnames_by_account_id(account_id) ⇒ Array<CustomCname>
Returns all custom cnames associated with the specified account ID.
-
#get_placement_constraint(placement_constraint_id:) ⇒ Class<PlacementConstraint>
Query the API for a placement constraint.
-
#get_placement_constraints ⇒ Array<PlacementConstraint>
Returns all placement constraints.
-
#get_placement_constraints_by_account_id(account_id) ⇒ Array<PlacementConstraint>
Returns all placement constraints associated with the specified account ID.
-
#get_routing_policies ⇒ Array<RoutingPolicy>
Returns all routing policies.
-
#get_routing_policies_by_account_id(account_id) ⇒ Array<RoutingPolicy>
Returns all routing policies associated with the specified account ID.
-
#get_routing_policy(policy_id:) ⇒ Class<RoutingPolicy>
Query the API for a routing policy.
-
#get_routing_policy_acceptance_config(policy_id:, format: "json") ⇒ String
Return Goss configuration.
-
#initialize(username:, password:, endpoint:, dynamic: false, retries: 5) ⇒ Client
constructor
A new instance of Client.
-
#perform_cluster_acceptance(type:, namespace:) ⇒ Class<AcceptanceResult>
Perform an acceptance test for a cluster.
-
#perform_cluster_site_acceptance(type:, namespace:, site:) ⇒ Class<AcceptanceResult>
Perform an acceptance test for a cluster site.
-
#perform_custom_cname_acceptance(cname_id:) ⇒ Class<AcceptanceResult>
Perform an acceptance test for a custom CNAME.
-
#perform_routing_policy_acceptance(policy_id:) ⇒ Class<AcceptanceResult>
Perform an acceptance test for a routing policy.
-
#ping? ⇒ Bool
Returns true if the endpoint is healthy.
- #post(path, params: {}) ⇒ Object
- #post_body(path, params: {}) ⇒ Object
Constructor Details
#initialize(username:, password:, endpoint:, dynamic: false, retries: 5) ⇒ Client
Returns a new instance of Client.
22 23 24 25 26 27 28 |
# File 'lib/cartography/client.rb', line 22 def initialize(username:, password:, endpoint:, dynamic: false, retries: 5) @username = username @password = password @endpoint = endpoint @dynamic = dynamic @retries = retries end |
Instance Attribute Details
#api_key ⇒ String (readonly)
The API key used for authentication
14 15 16 |
# File 'lib/cartography/client.rb', line 14 def api_key @api_key end |
#dynamic ⇒ Object (readonly)
Returns the value of attribute dynamic.
15 16 17 |
# File 'lib/cartography/client.rb', line 15 def dynamic @dynamic end |
#endpoint ⇒ String (readonly)
The API endpoint
14 15 16 |
# File 'lib/cartography/client.rb', line 14 def endpoint @endpoint end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
15 16 17 |
# File 'lib/cartography/client.rb', line 15 def password @password end |
#retries ⇒ Object (readonly)
Returns the value of attribute retries.
15 16 17 |
# File 'lib/cartography/client.rb', line 15 def retries @retries end |
#the ⇒ Integer (readonly)
number of retries for the request
14 15 16 |
# File 'lib/cartography/client.rb', line 14 def the @the end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
15 16 17 |
# File 'lib/cartography/client.rb', line 15 def username @username end |
#whether ⇒ Bool (readonly)
to return dynamic data
14 15 16 |
# File 'lib/cartography/client.rb', line 14 def whether @whether end |
Instance Method Details
#get(path, params: {}) ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/cartography/client.rb', line 292 def get(path, params: {}) retry_count ||= 0 resp = HTTP.basic_auth(user: username, pass: password).get("#{endpoint}/#{strip_path(path)}", params: default_params.merge(params)) raise ResponseError, resp.status.to_s unless resp.status.success? resp rescue *RETRY_ERRORS => e sleep 1 * retry_count retry if (retry_count += 1) < retries raise e end |
#get_body(path, params: {}) ⇒ Object
307 308 309 |
# File 'lib/cartography/client.rb', line 307 def get_body(path, params: {}) JSON.parse(get(path, params: default_params.merge(params || {})).body) end |
#get_cluster(type:, namespace:) ⇒ Array<Cluster>
Query the API for a specific cluster
169 170 171 172 |
# File 'lib/cartography/client.rb', line 169 def get_cluster(type:, namespace:) cluster = get_body("#{clusters_root_path}/#{type}/#{namespace}") Cartography::Cluster.new(cluster) end |
#get_cluster_acceptance_config(type:, namespace:, format: "json") ⇒ String
Return Goss configuration for a cluster
191 192 193 |
# File 'lib/cartography/client.rb', line 191 def get_cluster_acceptance_config(type:, namespace:, format: "json") get_body("#{clusters_root_path}/#{type}/#{namespace}/acceptance/config", params: {format: format}).to_s end |
#get_cluster_site(type:, namespace:, site:) ⇒ Cluster
Query the API for a specific cluster site by type, namespace and site
201 202 203 204 |
# File 'lib/cartography/client.rb', line 201 def get_cluster_site(type:, namespace:, site:) site = get_body("#{clusters_root_path}/#{type}/#{namespace}/#{site}") Cartography::Cluster::Site.new(site) end |
#get_cluster_site_acceptance_config(type:, namespace:, site:, format: "json") ⇒ String
Return Goss configuration for a cluster site
225 226 227 |
# File 'lib/cartography/client.rb', line 225 def get_cluster_site_acceptance_config(type:, namespace:, site:, format: "json") get_body("#{clusters_root_path}/#{type}/#{namespace}/#{site}/acceptance/config", params: {format: format}).to_s end |
#get_cluster_site_server_group(type:, namespace:, site:, server_group:) ⇒ ServerGroup
Query the API for a specific server group in a specific cluster site by type, namespace and site
252 253 254 255 |
# File 'lib/cartography/client.rb', line 252 def get_cluster_site_server_group(type:, namespace:, site:, server_group:) server_group = get_body("#{clusters_root_path}/#{type}/#{namespace}/#{site}/server-groups/#{server_group}") Cartography::Cluster::Site::ServerGroup.new(server_group) end |
#get_cluster_site_server_groups(type:, namespace:, site:) ⇒ Array<ServerGroup>
Query the API for all server groups in a specific cluster site by type, namespace and site
236 237 238 239 240 241 242 |
# File 'lib/cartography/client.rb', line 236 def get_cluster_site_server_groups(type:, namespace:, site:) server_groups = get_body("#{clusters_root_path}/#{type}/#{namespace}/#{site}/server-groups") server_groups.map do |sg| Cartography::Cluster::Site::ServerGroup.new(sg) end end |
#get_clusters ⇒ Array<Cluster>
Query the API for all clusters
144 145 146 147 148 149 150 |
# File 'lib/cartography/client.rb', line 144 def get_clusters clusters = get_body(clusters_root_path) clusters.map do |cluster| Cartography::Cluster.new(cluster) end end |
#get_clusters_by_type(type:) ⇒ Array<Cluster>
Query the API for clusters by type
156 157 158 159 160 161 162 |
# File 'lib/cartography/client.rb', line 156 def get_clusters_by_type(type:) clusters = get_body("#{clusters_root_path}/#{type}") clusters.map do |cluster| Cartography::Cluster.new(cluster) end end |
#get_custom_cname(cname_id:) ⇒ Class<CustomCnamey>
Query the API for a custom CNAME
64 65 66 67 68 |
# File 'lib/cartography/client.rb', line 64 def get_custom_cname(cname_id:) params = get_body("#{custom_cnames_root_path}/#{cname_id}") Cartography::CustomCname.new(params) end |
#get_custom_cname_acceptance_config(cname_id:, format: "json") ⇒ String
Return Goss configuration
85 86 87 |
# File 'lib/cartography/client.rb', line 85 def get_custom_cname_acceptance_config(cname_id:, format: "json") get_body("#{custom_cnames_root_path}/#{cname_id}/acceptance/config", params: {format: format}).to_s end |
#get_custom_cnames ⇒ Array<CustomCname>
Returns all custom CNAMEs
40 41 42 43 44 45 46 |
# File 'lib/cartography/client.rb', line 40 def get_custom_cnames cnames = get_body(custom_cnames_root_path) cnames.map do |cname| Cartography::CustomCname.new(cname) end end |
#get_custom_cnames_by_account_id(account_id) ⇒ Array<CustomCname>
Returns all custom cnames associated with the specified account ID
52 53 54 55 56 57 58 |
# File 'lib/cartography/client.rb', line 52 def get_custom_cnames_by_account_id(account_id) cnames = get_body(custom_cnames_root_path, params: {account_id: account_id}) cnames.map do |cname| Cartography::CustomCname.new(cname) end end |
#get_placement_constraint(placement_constraint_id:) ⇒ Class<PlacementConstraint>
Query the API for a placement constraint
284 285 286 287 288 |
# File 'lib/cartography/client.rb', line 284 def get_placement_constraint(placement_constraint_id:) params = get_body("#{placement_constraints_root_path}/#{placement_constraint_id}") Cartography::PlacementConstraint.new(params) end |
#get_placement_constraints ⇒ Array<PlacementConstraint>
Returns all placement constraints
260 261 262 263 264 265 266 |
# File 'lib/cartography/client.rb', line 260 def get_placement_constraints constraints = get_body(placement_constraints_root_path) constraints.map do |constraint| Cartography::PlacementConstraint.new(constraint) end end |
#get_placement_constraints_by_account_id(account_id) ⇒ Array<PlacementConstraint>
Returns all placement constraints associated with the specified account ID
272 273 274 275 276 277 278 |
# File 'lib/cartography/client.rb', line 272 def get_placement_constraints_by_account_id(account_id) constraints = get_body(placement_constraints_root_path, params: {account_id: account_id}) constraints.map do |constraint| Cartography::PlacementConstraint.new(constraint) end end |
#get_routing_policies ⇒ Array<RoutingPolicy>
Returns all routing policies
92 93 94 95 96 97 98 |
# File 'lib/cartography/client.rb', line 92 def get_routing_policies policies = get_body(routing_policies_root_path) policies.map do |policy| Cartography::RoutingPolicy.new(policy) end end |
#get_routing_policies_by_account_id(account_id) ⇒ Array<RoutingPolicy>
Returns all routing policies associated with the specified account ID
104 105 106 107 108 109 110 |
# File 'lib/cartography/client.rb', line 104 def get_routing_policies_by_account_id(account_id) policies = get_body(routing_policies_root_path, params: {account_id: account_id}) policies.map do |policy| Cartography::RoutingPolicy.new(policy) end end |
#get_routing_policy(policy_id:) ⇒ Class<RoutingPolicy>
Query the API for a routing policy
116 117 118 119 120 |
# File 'lib/cartography/client.rb', line 116 def get_routing_policy(policy_id:) params = get_body("#{routing_policies_root_path}/#{policy_id}") Cartography::RoutingPolicy.new(params) end |
#get_routing_policy_acceptance_config(policy_id:, format: "json") ⇒ String
Return Goss configuration
137 138 139 |
# File 'lib/cartography/client.rb', line 137 def get_routing_policy_acceptance_config(policy_id:, format: "json") get_body("#{routing_policies_root_path}/#{policy_id}/acceptance/config", params: {format: format}).to_s end |
#perform_cluster_acceptance(type:, namespace:) ⇒ Class<AcceptanceResult>
Perform an acceptance test for a cluster
179 180 181 182 183 |
# File 'lib/cartography/client.rb', line 179 def perform_cluster_acceptance(type:, namespace:) params = post_body("#{clusters_root_path}/#{type}/#{namespace}/acceptance") Cartography::AcceptanceResult.new(params) end |
#perform_cluster_site_acceptance(type:, namespace:, site:) ⇒ Class<AcceptanceResult>
Perform an acceptance test for a cluster site
212 213 214 215 216 |
# File 'lib/cartography/client.rb', line 212 def perform_cluster_site_acceptance(type:, namespace:, site:) params = post_body("#{clusters_root_path}/#{type}/#{namespace}/#{site}/acceptance") Cartography::AcceptanceResult.new(params) end |
#perform_custom_cname_acceptance(cname_id:) ⇒ Class<AcceptanceResult>
Perform an acceptance test for a custom CNAME
74 75 76 77 78 |
# File 'lib/cartography/client.rb', line 74 def perform_custom_cname_acceptance(cname_id:) params = post_body("#{custom_cnames_root_path}/#{cname_id}/acceptance") Cartography::AcceptanceResult.new(params) end |
#perform_routing_policy_acceptance(policy_id:) ⇒ Class<AcceptanceResult>
Perform an acceptance test for a routing policy
126 127 128 129 130 |
# File 'lib/cartography/client.rb', line 126 def perform_routing_policy_acceptance(policy_id:) params = post_body("#{routing_policies_root_path}/#{policy_id}/acceptance") Cartography::AcceptanceResult.new(params) end |
#ping? ⇒ Bool
Returns true if the endpoint is healthy
33 34 35 |
# File 'lib/cartography/client.rb', line 33 def ping? get_body("ping")["message"] == "pong" end |
#post(path, params: {}) ⇒ Object
311 312 313 314 315 316 317 |
# File 'lib/cartography/client.rb', line 311 def post(path, params: {}) resp = HTTP.basic_auth(user: username, pass: password).post("#{endpoint}/#{path}", params: default_params.merge(params)) raise ResponseError, resp.status.to_s unless resp.status.success? resp end |
#post_body(path, params: {}) ⇒ Object
319 320 321 |
# File 'lib/cartography/client.rb', line 319 def post_body(path, params: {}) JSON.parse(post(path, params: default_params.merge(params)).body) end |