Class: Datadog::Core::Remote::Transport::HTTP::Config::Response

Inherits:
Object
  • Object
show all
Includes:
Config::Response, Transport::HTTP::Response
Defined in:
lib/datadog/core/remote/transport/http/config.rb

Overview

Response from HTTP transport for remote configuration

Defined Under Namespace

Classes: DecodeError, KeyError, ParseError, TypeError

Instance Attribute Summary

Attributes included from Config::Response

#client_configs, #roots, #target_files, #targets

Instance Method Summary collapse

Methods included from Config::Response

#empty?

Methods included from Transport::HTTP::Response

#client_error?, #code, #internal_error?, #not_found?, #ok?, #payload, #server_error?, #unsupported?

Constructor Details

#initialize(http_response, options = {}) ⇒ Response

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/datadog/core/remote/transport/http/config.rb', line 39

def initialize(http_response, options = {}) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  super(http_response)

  begin
    payload = JSON.parse(http_response.payload, symbolize_names: true)
  rescue JSON::ParserError => e
    raise ParseError.new(:roots, e)
  end

  raise TypeError.new(Hash, payload) unless payload.is_a?(Hash)

  @empty = true if payload.empty?

  # TODO: these fallbacks should be improved
  roots = payload[:roots] || []
  targets = payload[:targets] || Base64.strict_encode64('{}')
  target_files = payload[:target_files] || []
  client_configs = payload[:client_configs] || []

  raise TypeError.new(Array, roots) unless roots.is_a?(Array)

  @roots = roots.map do |root|
    raise TypeError.new(String, root) unless root.is_a?(String)

    decoded = begin
      Base64.strict_decode64(root) # TODO: unprocessed, don't symbolize_names
    rescue ArgumentError
      raise DecodeError.new(:roots, root)
    end

    parsed = begin
      JSON.parse(decoded)
    rescue JSON::ParserError
      raise ParseError.new(:roots, decoded)
    end

    # TODO: perform more processing to validate content. til then, no freeze

    parsed
  end

  raise TypeError.new(String, targets) unless targets.is_a?(String)

  @targets = begin
    decoded = begin
      Base64.strict_decode64(targets)
    rescue ArgumentError
      raise DecodeError.new(:targets, targets)
    end

    parsed = begin
      JSON.parse(decoded) # TODO: unprocessed, don't symbolize_names
    rescue JSON::ParserError
      raise ParseError.new(:targets, decoded)
    end

    # TODO: perform more processing to validate content. til then, no freeze

    parsed
  end

  raise TypeError.new(Array, target_files) unless target_files.is_a?(Array)

  @target_files = target_files.map do |h|
    raise TypeError.new(Hash, h) unless h.is_a?(Hash)
    raise KeyError.new(:raw) unless h.key?(:raw) # rubocop:disable Style/RaiseArgs
    raise KeyError.new(:path) unless h.key?(:path) # rubocop:disable Style/RaiseArgs

    raw = h[:raw]

    raise TypeError.new(String, raw) unless raw.is_a?(String)

    content = begin
      Base64.strict_decode64(raw)
    rescue ArgumentError
      raise DecodeError.new(:target_files, raw)
    end

    {
      path: h[:path].freeze,
      content: StringIO.new(content.freeze),
    }
  end.freeze

  @client_configs = client_configs.map do |s|
    raise TypeError.new(String, s) unless s.is_a?(String)

    s.freeze
  end.freeze
end

Instance Method Details

#inspectObject



130
131
132
133
134
135
136
137
138
# File 'lib/datadog/core/remote/transport/http/config.rb', line 130

def inspect
  "#{super}, #{
    {
      roots: @roots,
      targets: @targets,
      target_files: @target_files,
      client_configs: @client_configs,
    }}"
end