|
| 1 | +require 'fog/compute' |
| 2 | +require 'fog/cloudsigma/connection' |
| 3 | + |
| 4 | + |
| 5 | +module Fog |
| 6 | + module Compute |
| 7 | + class CloudSigma < Fog::Service |
| 8 | + requires :cloudsigma_password, :cloudsigma_username |
| 9 | + recognizes :cloudsigma_password, :cloudsigma_username |
| 10 | + |
| 11 | + model_path 'fog/cloudsigma/models' |
| 12 | + request_path 'fog/cloudsigma/requests' |
| 13 | + |
| 14 | + model :volume |
| 15 | + collection :volumes |
| 16 | + request :create_volume |
| 17 | + request :get_volume |
| 18 | + request :list_volumes |
| 19 | + request :update_volume |
| 20 | + request :delete_volume |
| 21 | + request :clone_volume |
| 22 | + |
| 23 | + model :lib_volume |
| 24 | + collection :lib_volumes |
| 25 | + request :get_lib_volume |
| 26 | + request :list_lib_volumes |
| 27 | + |
| 28 | + model :ipconf |
| 29 | + model :nic |
| 30 | + model :mountpoint |
| 31 | + model :server |
| 32 | + collection :servers |
| 33 | + request :create_server |
| 34 | + request :get_server |
| 35 | + request :list_servers |
| 36 | + request :update_server |
| 37 | + request :delete_server |
| 38 | + request :start_server |
| 39 | + request :stop_server |
| 40 | + request :open_vnc |
| 41 | + request :close_vnc |
| 42 | + request :clone_server |
| 43 | + |
| 44 | + model :ip |
| 45 | + collection :ips |
| 46 | + request :list_ips |
| 47 | + request :get_ip |
| 48 | + |
| 49 | + model :vlan |
| 50 | + collection :vlans |
| 51 | + request :list_vlans |
| 52 | + request :get_vlan |
| 53 | + request :update_vlan |
| 54 | + |
| 55 | + model :subscription |
| 56 | + collection :subscriptions |
| 57 | + request :list_subscriptions |
| 58 | + request :get_subscription |
| 59 | + request :create_subscription |
| 60 | + request :extend_subscription |
| 61 | + |
| 62 | + model :price_calculation |
| 63 | + request :calculate_subscription_price |
| 64 | + |
| 65 | + model :profile |
| 66 | + request :get_profile |
| 67 | + request :update_profile |
| 68 | + |
| 69 | + model :balance |
| 70 | + request :get_balance |
| 71 | + |
| 72 | + model :current_usage |
| 73 | + request :get_current_usage |
| 74 | + |
| 75 | + model :pricing |
| 76 | + request :get_pricing |
| 77 | + |
| 78 | + |
| 79 | + module CommonMockAndReal |
| 80 | + def initialize(options={}) |
| 81 | + @init_options = options |
| 82 | + |
| 83 | + setup_connection(options) |
| 84 | + end |
| 85 | + |
| 86 | + def profile |
| 87 | + response = get_profile |
| 88 | + Profile.new(response.body) |
| 89 | + end |
| 90 | + |
| 91 | + def balance |
| 92 | + response = get_balance |
| 93 | + |
| 94 | + Balance.new(response.body) |
| 95 | + end |
| 96 | + |
| 97 | + def current_usage |
| 98 | + response = get_current_usage |
| 99 | + |
| 100 | + CurrentUsage.new(response.body['usage']) |
| 101 | + end |
| 102 | + |
| 103 | + def currency |
| 104 | + # Cache since currency does not change |
| 105 | + @currency ||= profile.currency |
| 106 | + |
| 107 | + end |
| 108 | + |
| 109 | + def pricing |
| 110 | + resp = get_princing(currency) |
| 111 | + |
| 112 | + resp.body['objects'] |
| 113 | + end |
| 114 | + |
| 115 | + def current_pricing_levels |
| 116 | + resp = get_pricing(currency) |
| 117 | + |
| 118 | + resp.body['current'] |
| 119 | + end |
| 120 | + |
| 121 | + def next_pricing_levels |
| 122 | + resp = get_pricing(currency) |
| 123 | + |
| 124 | + resp.body['next'] |
| 125 | + end |
| 126 | + |
| 127 | + def subscription_pricing |
| 128 | + resp = get_pricing(currency, true) |
| 129 | + |
| 130 | + current_levels = resp.body['current'] |
| 131 | + current_prices = resp.body['objects'] |
| 132 | + |
| 133 | + current_pricing_pairs = current_levels.map do |resource, level| |
| 134 | + price_for_resource_and_level = current_prices.detect do |price| |
| 135 | + price['resource'] == resource |
| 136 | + end |
| 137 | + price_for_resource_and_level ||= {} |
| 138 | + |
| 139 | + [resource, price_for_resource_and_level] |
| 140 | + end |
| 141 | + |
| 142 | + Pricing.new(Hash[current_pricing_pairs]) |
| 143 | + end |
| 144 | + |
| 145 | + def current_pricing |
| 146 | + resp = get_pricing(currency) |
| 147 | + |
| 148 | + current_levels = resp.body['current'] |
| 149 | + current_prices = resp.body['objects'] |
| 150 | + |
| 151 | + current_pricing_pairs = current_levels.map do |resource, level| |
| 152 | + price_for_resource_and_level = current_prices.detect do |price| |
| 153 | + price['level'] == level && price['resource'] == resource |
| 154 | + end |
| 155 | + price_for_resource_and_level ||= {} |
| 156 | + |
| 157 | + [resource, price_for_resource_and_level] |
| 158 | + end |
| 159 | + |
| 160 | + Pricing.new(Hash[current_pricing_pairs]) |
| 161 | + end |
| 162 | + |
| 163 | + def next_pricing |
| 164 | + resp = get_pricing(currency) |
| 165 | + |
| 166 | + current_levels = resp.body['next'] |
| 167 | + current_prices = resp.body['objects'] |
| 168 | + |
| 169 | + current_pricing_pairs = current_levels.map do |resource, level| |
| 170 | + price_for_resource_and_level = current_prices.detect do |price| |
| 171 | + price['level'] == level && price['resource'] == resource |
| 172 | + end |
| 173 | + price_for_resource_and_level ||= {} |
| 174 | + |
| 175 | + [resource, price_for_resource_and_level] |
| 176 | + end |
| 177 | + |
| 178 | + Pricing.new(Hash[current_pricing_pairs]) |
| 179 | + end |
| 180 | + |
| 181 | + end |
| 182 | + |
| 183 | + class Mock |
| 184 | + include Collections |
| 185 | + include CommonMockAndReal |
| 186 | + include Fog::CloudSigma::CloudSigmaConnection::Mock |
| 187 | + require 'fog/cloudsigma/mock_data' |
| 188 | + |
| 189 | + def self.data |
| 190 | + @data ||= Hash.new do |hash, key| |
| 191 | + hash[key] = mock_data |
| 192 | + end |
| 193 | + end |
| 194 | + |
| 195 | + def self.random_uuid |
| 196 | + # Insert '4' at 13th position and 'a' at 17th as per uuid4 spec |
| 197 | + hex = Fog::Mock.random_hex(30).insert(12,'4').insert(16, 'a') |
| 198 | + # Add dashes |
| 199 | + "#{hex[0...8]}-#{hex[8...12]}-#{hex[12...16]}-#{hex[16...20]}-#{hex[20..32]}" |
| 200 | + end |
| 201 | + |
| 202 | + def self.random_mac |
| 203 | + (0..5).map{Fog::Mock.random_hex(2)}.join(':') |
| 204 | + end |
| 205 | + |
| 206 | + def data |
| 207 | + self.class.data[:test] |
| 208 | + end |
| 209 | + end |
| 210 | + |
| 211 | + class Real |
| 212 | + include Collections |
| 213 | + include CommonMockAndReal |
| 214 | + include Fog::CloudSigma::CloudSigmaConnection::Real |
| 215 | + |
| 216 | + end |
| 217 | + |
| 218 | + end |
| 219 | + end |
| 220 | + |
| 221 | +end |
0 commit comments