Class: Cartography::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_keyString (readonly)

The API key used for authentication

Returns:

  • (String)

    the current value of api_key



14
15
16
# File 'lib/cartography/client.rb', line 14

def api_key
  @api_key
end

#dynamicObject (readonly)

Returns the value of attribute dynamic.



15
16
17
# File 'lib/cartography/client.rb', line 15

def dynamic
  @dynamic
end

#endpointString (readonly)

The API endpoint

Returns:

  • (String)

    the current value of endpoint



14
15
16
# File 'lib/cartography/client.rb', line 14

def endpoint
  @endpoint
end

#passwordObject (readonly)

Returns the value of attribute password.



15
16
17
# File 'lib/cartography/client.rb', line 15

def password
  @password
end

#retriesObject (readonly)

Returns the value of attribute retries.



15
16
17
# File 'lib/cartography/client.rb', line 15

def retries
  @retries
end

#theInteger (readonly)

number of retries for the request

Returns:

  • (Integer)

    the current value of the



14
15
16
# File 'lib/cartography/client.rb', line 14

def the
  @the
end

#usernameObject (readonly)

Returns the value of attribute username.



15
16
17
# File 'lib/cartography/client.rb', line 15

def username
  @username
end

#whetherBool (readonly)

to return dynamic data

Returns:

  • (Bool)

    the current value of whether



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

Parameters:

  • type (String)

    the cluster type, such as realtime

  • namespace (String)

    the cluster namespace, such as main

Returns:



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

Parameters:

  • type (String)

    the cluster type, such as realtime

  • namespace (String)

    the cluster namespace, such as main

  • format (String) (defaults to: "json")

    the format of the result, such as json or yaml

Returns:

  • (String)

    the Goss configuration



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

Parameters:

  • type (String)

    the cluster type, such as realtime

  • namespace (String)

    the cluster namespace, such as main

  • site (String)

    the cluster site, such as eu-west-1-A

Returns:



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

Parameters:

  • type (String)

    the cluster type, such as realtime

  • namespace (String)

    the cluster namespace, such as main

  • site (String)

    the cluster site, such as eu-west-1-A

  • format (String) (defaults to: "json")

    the format of the result, such as json or yaml

Returns:

  • (String)

    the Goss configuration



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

Parameters:

  • type (String)

    the cluster type, such as realtime

  • namespace (String)

    the cluster namespace, such as main

  • site (String)

    the cluster site, such as eu-west-1-A

  • server_group (String)

    the server group, such as core

Returns:

  • (ServerGroup)


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

Parameters:

  • type (String)

    the cluster type, such as realtime

  • namespace (String)

    the cluster namespace, such as main

  • site (String)

    the cluster site, such as eu-west-1-A

Returns:

  • (Array<ServerGroup>)


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_clustersArray<Cluster>

Query the API for all clusters

Returns:



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

Parameters:

  • type (String)

    the cluster type, such as realtime

Returns:



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

Parameters:

  • cname_id (String)

    the custom CNAME ID

Returns:

  • (Class<CustomCnamey>)

    a CustomCname instance



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

Parameters:

  • cname_id (String)

    the custom CNAME ID

  • format (String) (defaults to: "json")

    the format of the result, such as json or yaml

Returns:

  • (String)

    the 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_cnamesArray<CustomCname>

Returns all custom CNAMEs

Returns:



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

Parameters:

  • account_id (String)

    the account ID

Returns:



52
53
54
55
56
57
58
# File 'lib/cartography/client.rb', line 52

def ()
  cnames = get_body(custom_cnames_root_path, params: {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

Parameters:

  • placement_constraint_id (String)

    the placement constraint ID

Returns:



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_constraintsArray<PlacementConstraint>

Returns all placement constraints

Returns:



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

Parameters:

  • account_id (String)

    the account ID

Returns:



272
273
274
275
276
277
278
# File 'lib/cartography/client.rb', line 272

def ()
  constraints = get_body(placement_constraints_root_path, params: {account_id: })

  constraints.map do |constraint|
    Cartography::PlacementConstraint.new(constraint)
  end
end

#get_routing_policiesArray<RoutingPolicy>

Returns all routing policies

Returns:



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

Parameters:

  • account_id (String)

    the account ID

Returns:



104
105
106
107
108
109
110
# File 'lib/cartography/client.rb', line 104

def ()
  policies = get_body(routing_policies_root_path, params: {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

Parameters:

  • policy_id (String)

    the routing policy ID

Returns:



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

Parameters:

  • policy_id (String)

    the routing policy ID

  • format (String) (defaults to: "json")

    the format of the result, such as json or yaml

Returns:

  • (String)

    the 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

Parameters:

  • type (String)

    the cluster type, such as realtime

  • namespace (String)

    the cluster namespace, such as main

Returns:



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

Parameters:

  • type (String)

    the cluster type, such as realtime

  • namespace (String)

    the cluster namespace, such as main

  • site (String)

    the cluster site, such as eu-west-1-A

Returns:



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

Parameters:

  • cname_id (String)

    the custom CNAME ID

Returns:



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

Parameters:

  • policy_id (String)

    the routing policy ID

Returns:



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

Returns:

  • (Bool)


33
34
35
# File 'lib/cartography/client.rb', line 33

def ping?
  get_body("ping")["message"] == "pong"
end

#post(path, params: {}) ⇒ Object

Raises:



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