Class: Conversant::V3::Services::CDN::Domain

Inherits:
Object
  • Object
show all
Defined in:
lib/conversant/v3/services/cdn/domain.rb

Overview

Domain management service for CDN domains

Provides comprehensive domain management functionality including:

  • Domain listing and filtering
  • Domain creation and configuration
  • Domain lookup and search
  • Storage usage calculations

Examples:

Basic domain operations

domains = cdn.domain.all
domain = cdn.domain.find(123)
domain = cdn.domain.find_by(name: 'example.com')

# Create new domain
domain_id = cdn.domain.create({
  businessScenarioType: 1,
  name: "cdn.example.com",
  origins: [{ url_prefix: "1.2.3.4" }],
  redirect_http_to_https: true,
  http2_enabled: true
})

Since:

  • 1.0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Domain

Initialize domain service

Parameters:

  • parent (CDN)

    the parent CDN service instance

Since:

  • 1.0.1



37
38
39
# File 'lib/conversant/v3/services/cdn/domain.rb', line 37

def initialize(parent)
  @parent = parent
end

Instance Attribute Details

#parentCDN (readonly)

Returns the parent CDN service instance.

Returns:

  • (CDN)

    the parent CDN service instance

Since:

  • 1.0.1



32
33
34
# File 'lib/conversant/v3/services/cdn/domain.rb', line 32

def parent
  @parent
end

Instance Method Details

#allArray<OpenStruct>, Array

Get all domains for the customer

Retrieves all domains associated with the customer account. Results are cached in Redis for 10 minutes to improve performance.

Examples:

Get all domains

domains = cdn.domain.all
domains.each do |domain|
  puts "#{domain.name} (ID: #{domain.id}) - Status: #{domain.status}"
  puts "  Business Type: #{domain.businessScenarioType}"
  puts "  Current Usage: #{domain.current_disk_usage} bytes"
end

Returns:

  • (Array<OpenStruct>, Array)

    array of domain objects with properties like id, name, status, etc. Returns empty array on error or if no domains exist.

Since:

  • 1.0.1



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/conversant/v3/services/cdn/domain.rb', line 58

def all
  key = "customer.#{@parent.customer_id}.domain.all"
  domains = redis.get(key)

  if domains.nil?
    response = @parent.send(:call, 'GET', '/api/delivery_domains?pageSize=-1&pageNumber=-1')
    return [] if response.nil?

    response = JSON.parse(response)
    if response&.[]('list')
      domains = response['list'].to_json
      redis.set(key, domains, ex: 600)
    else
      return []
    end
  end

  JSON.parse(domains).map do |item|
    OpenStruct.new(item.merge(customer_id: @parent.customer_id))
  end
rescue StandardError => e
  logger.error "#{@parent.send(:identifier)}.METHOD:#{__method__}.EXCEPTION:#{e.message}"
  []
end

#create(payload) ⇒ Integer?

Create a new CDN domain

Creates a new CDN domain with the specified configuration. Automatically clears the domain cache upon successful creation.

Examples:

Create a CDN domain

domain_id = cdn.domain.create({
  businessScenarioType: 1,
  name: "cdn.example.com",
  origins: [{ url_prefix: "203.0.113.10" }],
  redirect_http_to_https: true,
  http2_enabled: true,
  streaming_service: false,
  disabled: false,
  hwc_enabled: false,
  multiCDN: []
})

if domain_id
  puts "Domain created with ID: #{domain_id}"
else
  puts "Failed to create domain"
end

Create a storage domain

domain_id = cdn.domain.create({
  businessScenarioType: 2,
  name: "storage.example.com",
  ftpPassword: "secure_password",
  ftpPasswordRepeat: "secure_password",
  origins: [{ url_prefix: "203.0.113.20" }]
})

Parameters:

  • payload (Hash)

    domain configuration

Options Hash (payload):

  • :businessScenarioType (Integer)

    business scenario (1=CDN, 2=Storage)

  • :name (String)

    domain name (e.g., "cdn.example.com")

  • :lfdName (String)

    load balancer domain name (optional)

  • :origin_url (String)

    origin server URL (optional)

  • :origin_proto (String)

    origin protocol (optional)

  • :redirect_http_to_https (Boolean)

    enable HTTP to HTTPS redirect

  • :http2_enabled (Boolean)

    enable HTTP/2 support

  • :streaming_service (Boolean)

    enable streaming capabilities

  • :disabled (Boolean)

    initial disabled state

  • :hwc_enabled (Boolean)

    enable hardware acceleration

  • :multiCDN (Array)

    multi-CDN configuration

  • :origins (Array<Hash>)

    array of origin servers

  • :ftpPassword (String)

    FTP password (for storage domains)

  • :ftpPasswordRepeat (String)

    FTP password confirmation

Returns:

  • (Integer, nil)

    the new domain ID if successful, nil on error

Since:

  • 1.0.1



173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/conversant/v3/services/cdn/domain.rb', line 173

def create(payload)
  response = @parent.send(:call, 'POST', '/api/v6/delivery_domain', payload)
  return nil if response.nil?

  response = JSON.parse(response)
  return nil unless response.to_i.positive?

  # Clear cache after creation
  redis.del("customer.#{@parent.customer_id}.domain.all")
  response
rescue StandardError => e
  logger.error "#{@parent.send(:identifier)}.METHOD:#{__method__}.EXCEPTION:#{e.message}"
  nil
end

#find(domain_id) ⇒ OpenStruct?

Find domain by ID

Examples:

Find domain by ID

domain = cdn.domain.find(12345)
if domain
  puts "Found domain: #{domain.name}"
  puts "Status: #{domain.status}"
else
  puts "Domain not found"
end

Parameters:

  • domain_id (Integer, String)

    the domain ID to search for

Returns:

  • (OpenStruct, nil)

    domain object if found, nil otherwise

Since:

  • 1.0.1



99
100
101
# File 'lib/conversant/v3/services/cdn/domain.rb', line 99

def find(domain_id)
  all.find { |domain| domain.id == domain_id }
end

#find_by(params) ⇒ OpenStruct?

Find domain by attributes

Examples:

Find domain by name

domain = cdn.domain.find_by(name: 'cdn.example.com')
if domain
  puts "Found domain with ID: #{domain.id}"
end

Parameters:

  • params (Hash)

    search parameters

Options Hash (params):

  • :name (String)

    domain name to search for

Returns:

  • (OpenStruct, nil)

    first domain matching the criteria, nil if not found

Since:

  • 1.0.1



117
118
119
# File 'lib/conversant/v3/services/cdn/domain.rb', line 117

def find_by(params)
  all.find { |domain| domain.name == params[:name] }
end

#total_current_disk_usageInteger

Calculate total disk usage for storage domains

Calculates the total current disk usage across all active storage domains (businessScenarioType=2, status=1) for the customer.

Examples:

Get total storage usage

total_bytes = cdn.domain.total_current_disk_usage
total_gb = total_bytes / (1024 ** 3)
puts "Total storage usage: #{total_gb} GB"

Returns:

  • (Integer)

    total disk usage in bytes, 0 if no storage domains or on error

Since:

  • 1.0.1



201
202
203
204
205
206
207
208
# File 'lib/conversant/v3/services/cdn/domain.rb', line 201

def total_current_disk_usage
  all.select do |item|
    item.businessScenarioType == 2 && item.status == 1
  end.map(&:current_disk_usage).sum
rescue StandardError => e
  logger.error "#{@parent.send(:identifier)}.METHOD:#{__method__}.EXCEPTION:#{e.message}"
  0
end