Module: Fog::Compute::Terremark::Mock

Includes:
Common
Defined in:
lib/fog/compute/terremark/mock.rb,
lib/fog/compute/terremark/requests/get_vdc.rb,
lib/fog/compute/terremark/requests/get_network.rb,
lib/fog/compute/terremark/requests/get_public_ips.rb,
lib/fog/compute/terremark/requests/get_organization.rb,
lib/fog/compute/terremark/requests/get_organizations.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#default_organization_id

Class Method Details

.error_headersObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/fog/compute/terremark/mock.rb', line 92

def self.error_headers
  {
      "X-Powered-By" => "ASP.NET",
      "Date" =>  Time.now.to_s,
      "Content-Type" => "text/html",
      "Content-Length" => "0",
      "Server" => "Microsoft-IIS/7.0",
      "Cache-Control" => "private"
  }
end

.headers(body, content_type) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fog/compute/terremark/mock.rb', line 107

def self.headers(body, content_type)
  {
      "X-Powered-By" => "ASP.NET",
      "Date" =>  Time.now.to_s,
      "Content-Type" =>  content_type,
      "Content-Length" =>  body.to_s.length,
      "Server" => "Microsoft-IIS/7.0",
      "Set-Cookie" => "vcloud-token=ecb37bfc-56f0-421d-97e5-bf2gdf789457; path=/",
      "Cache-Control" => "private"
  }
end

.mock_dataObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
# File 'lib/fog/compute/terremark/mock.rb', line 7

def self.mock_data
  {
      :organizations =>
          [
              {
                  :info => {
                      :name => "Boom Inc.",
                      :id => 1
                  },
                  :vdcs => [
                      { :id => 21,
                        :name => "Boomstick",
                        :storage => { :used => 105, :allocated => 200 },
                        :cpu => { :allocated => 10000 },
                        :memory => { :allocated => 20480 },
                        :networks => [
                            { :id => 31,
                              :name => "1.2.3.0/24",
                              :subnet => "1.2.3.0/24",
                              :gateway => "1.2.3.1",
                              :netmask => "255.255.255.0",
                              :fencemode => "isolated"
                            },
                            { :id => 32,
                              :name => "4.5.6.0/24",
                              :subnet => "4.5.6.0/24",
                              :gateway => "4.5.6.1",
                              :netmask => "255.255.255.0",
                              :fencemode => "isolated"
                            },
                        ],
                        :vms => [
                            { :id => 41,
                              :name => "Broom 1"
                            },
                            { :id => 42,
                              :name => "Broom 2"
                            },
                            { :id => 43,
                              :name => "Email!"
                            }
                        ],
                        :public_ips => [
                            { :id => 51,
                              :name => "99.1.2.3"
                            },
                            { :id => 52,
                              :name => "99.1.2.4"
                            },
                            { :id => 53,
                              :name => "99.1.9.7"
                            }
                        ]
                      },
                      { :id => 22,
                        :storage => { :used => 40, :allocated => 150 },
                        :cpu => { :allocated => 1000 },
                        :memory => { :allocated => 2048 },
                        :name => "Rock-n-Roll",
                        :networks => [
                            { :id => 33,
                              :name => "7.8.9.0/24",
                              :subnet => "7.8.9.0/24",
                              :gateway => "7.8.9.1",
                              :netmask => "255.255.255.0",
                              :fencemode => "isolated"
                            }
                        ],
                        :vms => [
                            { :id => 44,
                              :name => "Master Blaster"
                            }
                        ],
                        :public_ips => [
                            { :id => 54,
                              :name => "99.99.99.99"
                            }
                        ]
                      }
                  ]
              }
          ]
  }
end

.statusObject



119
120
121
# File 'lib/fog/compute/terremark/mock.rb', line 119

def self.status
  200
end

.unathorized_statusObject



103
104
105
# File 'lib/fog/compute/terremark/mock.rb', line 103

def self.unathorized_status
  401
end

Instance Method Details

#get_network(network_id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
# File 'lib/fog/compute/terremark/requests/get_network.rb', line 25

def get_network(network_id)
  network_id = network_id.to_i
  response = Excon::Response.new
  network = self.data[:organizations].map { |org| org[:vdcs].map { |vdc| vdc[:networks] } }.flatten.find { |network| network[:id] == network_id }
  if network

    body = { "links" => [],
             "type" => "application/vnd.vmware.vcloud.network+xml",
             "href" => "#{@base_url}/network/#{network_id}" }

    network.each_key do |key|
      body[key.to_s] = network[key]
    end

    link = { "name" => "IP Addresses",
             "rel"  => "down",
             "type" => "application/xml" }
    link["href"] = case self
                     when Fog::Terremark::Ecloud::Mock
                       "#{@base_url}/extensions/network/#{network_id}/ips"
                     when Fog::Terremark::Vcloud::Mock
                       "#{@base_url}/network/#{network_id}/ipAddresses"
                   end
    body["links"] << link

    response.status = 200
    response.body = body
    response.headers = Fog::Compute::Terremark::Mock.headers(response.body,
                                                            case self
                                                              when Fog::Terremark::Ecloud::Mock
                                                                "application/vnd.vmware.vcloud.network+xml"
                                                              when Fog::Terremark::Vcloud::Mock
                                                                "application/xml; charset=utf-8"
                                                            end
    )
  else
    response.status = Fog::Compute::Terremark::Mock.unathorized_status
    response.headers = Fog::Compute::Terremark::Mock.error_headers
  end

  response
end

#get_organization(organization_id) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fog/compute/terremark/requests/get_organization.rb', line 32

def get_organization(organization_id)
  organization_id = organization_id.to_i
  response = Excon::Response.new

  if org = self.data[:organizations].find { |org| org[:info][:id] == organization_id }

    body = { "name" => org[:info][:name],
             "href" => "#{@base_url}/org/#{org[:info][:id]}",
             "Links" => [] }

    body["Links"] = case self
                      when Fog::Terremark::Vcloud::Mock
                        _vdc_links(org[:vdcs][0])
                      when Fog::Terremark::Ecloud::Mock
                        org[:vdcs].map do |vdc|
                          _vdc_links(vdc)
                        end.flatten
                    end

    response.status = 200
    response.body = body
    response.headers = Fog::Compute::Terremark::Mock.headers(response.body, "application/vnd.vmware.vcloud.org+xml")
  else
    response.status = Fog::Compute::Terremark::Mock.unathorized_status
    response.headers = Fog::Compute::Terremark::Mock.error_headers
  end

  response
end

#get_organizationsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fog/compute/terremark/requests/get_organizations.rb', line 29

def get_organizations
  response = Excon::Response.new
  org_list = self.data[:organizations].map do |organization|
    { "name" => organization[:info][:name],
      "href" => "#{@base_url}/org/#{organization[:info][:id]}",
      "type" => "application/vnd.vmware.vcloud.org+xml"
    }
  end
  response.body = { "OrgList" => org_list }
  response.status = 200
  response.headers = Fog::Compute::Terremark::Mock.headers(response.body, "application/vnd.vmware.vcloud.orgList+xml")
  response
end

#get_public_ips(vdc_id) ⇒ Object



31
32
33
34
35
36
37
38
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
# File 'lib/fog/compute/terremark/requests/get_public_ips.rb', line 31

def get_public_ips(vdc_id)
  vdc_id = vdc_id.to_i
  response = Excon::Response.new

  vdc = self.data[:organizations].map { |org| org[:vdcs] }.flatten.find { |vdc| vdc[:id] == vdc_id }
  if vdc
    body = { "PublicIpAddresses" => [] }
    vdc[:public_ips].each do |ip|
      ip = { "name" => ip[:name],
             "href" => case self
                         when Fog::Terremark::Ecloud::Mock
                           "#{@base_url}/extensions/publicIp/#{ip[:id]}"
                         when Fog::Terremark::Vcloud::Mock
                           "#{@base_url}/PublicIps/#{ip[:id]}"
                       end,
             "id"   => ip[:id].to_s }
      body["PublicIpAddresses"] << ip
    end
    response.status = 200
    response.body = body
    response.headers = Fog::Compute::Terremark::Mock.headers(response.body,
                                                            case self
                                                              when Fog::Terremark::Ecloud::Mock
                                                                "application/vnd.tmrk.ecloud.publicIpsList+xml"
                                                              when Fog::Terremark::Vcloud::Mock
                                                                "application/xml; charset=utf-8"
                                                            end
    )
  else
    response.status = Fog::Compute::Terremark::Mock.unathorized_status
    response.headers = Fog::Compute::Terremark::Mock.error_headers
  end

  response
end

#get_vdc(vdc_id) ⇒ Object



33
34
35
36
37
38
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fog/compute/terremark/requests/get_vdc.rb', line 33

def get_vdc(vdc_id)
  vdc_id = vdc_id.to_i
  response = Excon::Response.new

  vdc = self.data[:organizations].map { |org| org[:vdcs] }.flatten.find { |vdc| vdc[:id] == vdc_id }
  if vdc
    body = {
        "name" => vdc[:name],
        "href" => "#{@base_url}/vdc/#{vdc[:id]}",
        "StorageCapacity" => {},
        "ComputeCapacity" => {
            "InstantiatedVmsQuota" => {},
            "DeployedVmsQuota" => {},
            "Cpu" => {},
            "Memory" => {}
        },
        "ResourceEntities" => [],
        "AvailableNetworks" => [],
        "links" => []
    }

    case self
      when Fog::Terremark::Ecloud::Mock
        body["StorageCapacity"] = { "Units" => "bytes * 10^9" }
        vdc[:storage].each { |k,v| body["StorageCapacity"][k.to_s.capitalize] = v.to_s }

        body["ComputeCapacity"] = {
            "InstantiatedVmsQuota" => {
                "Limit" => "-1",
                "Used" => "-1"
            },
            "DeployedVmsQuota" => {
                "Limit" => "-1",
                "Used" => "-1"
            },
            "Cpu" => {
                "Units" => "hz * 10^6"
            },
            "Memory" => {
                "Units" => "bytes * 2^20"
            }
        }

        [:cpu, :memory].each do |key|
          vdc[key].each { |k,v| body["ComputeCapacity"][key.to_s.capitalize][k.to_s.capitalize] = v.to_s }
        end

        body["links"] << {
            "name" => "Public IPs",
            "href" => "#{@base_url}/extensions/vdc/#{vdc[:id]}/publicIps",
            "rel"  => "down",
            "type" => "application/vnd.tmrk.ecloud.publicIpsList+xml"
        }

        body["links"] << {
            "name" => "Internet Services",
            "href" => "#{@base_url}/extensions/vdc/#{vdc[:id]}/internetServices",
            "rel"  => "down",
            "type" => "application/vnd.tmrk.ecloud.internetServicesList+xml"
        }

        body["links"] << {
            "name" => "Firewall Access List",
            "href" => "#{@base_url}/extensions/vdc/#{vdc[:id]}/firewallAcls",
            "rel"  => "down",
            "type" => "application/vnd.tmrk.ecloud.firewallAclsList+xml"
        }

      when Fog::Terremark::Vcloud::Mock
        body["links"] << {
            "name" => "Public IPs",
            "href" => "#{@base_url}/vdc/#{vdc[:id]}/publicIps",
            "rel"  => "down",
            "type" => "application/xml"
        }

        body["links"] << {
            "name" => "Internet Services",
            "href" => "#{@base_url}/vdc/#{vdc[:id]}/internetServices",
            "rel"  => "down",
            "type" => "application/xml"
        }
    end

    vdc[:vms].each do |vm|
      body["ResourceEntities"] << {
          "name" => vm[:name],
          "href" => "#{@base_url}/vapp/#{vm[:id]}",
          "type" => "application/vnd.vmware.vcloud.vApp+xml"
      }
    end

    vdc[:networks].each do |network|
      body["AvailableNetworks"] << {
          "name" => network[:name],
          "href" => "#{@base_url}/network/#{network[:id]}",
          "type" => "application/vnd.vmware.vcloud.network+xml"
      }
    end

    body["links"] << {
        "name" => vdc[:name],
        "href" => "#{@base_url}/vdc/#{vdc[:id]}/catalog",
        "rel"  => "down",
        "type" => "application/vnd.vmware.vcloud.catalog+xml"
    }

    response.status = 200
    response.body = body
    response.headers = Fog::Compute::Terremark::Mock.headers(response.body, "application/vnd.vmware.vcloud.org+xml")
  else
    response.status = Fog::Compute::Terremark::Mock.unathorized_status
    response.headers = Fog::Compute::Terremark::Mock.error_headers
  end

  response
end

#initialize(_options = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/fog/compute/terremark/mock.rb', line 123

def initialize(_options={})
  self.class.instance_eval '
    def self.data
      @data ||= Hash.new do |hash, key|
        hash[key] = Fog::Compute::Terremark::Mock.mock_data
      end
    end'
  self.class.instance_eval '
    def self.reset
      @data = nil
    end

    def self.reset_data(keys=data.keys)
      for key in [*keys]
        data.delete(key)
      end
    end'
end