zcloudjp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 999b24291ca788a4a552c74fbd25d41c9b928730
4
+ data.tar.gz: 9626d40a8350171fe8f275237ad5df04301725c1
5
+ SHA512:
6
+ metadata.gz: 2e19331e746ab02fa971e4c9e7d9febff4b2a47ebc2a38565493bd2e063250da58065f7cf3d80009d97f4c85065c53ff2fb08ae54eae9df8c81550db6668d5d3
7
+ data.tar.gz: c0c631614ffc3b0542350f2462d47fec65c3cf25a6b88934840e2e79e98780055773fa54efc172a40bfd47418979fec0ed8cfc0ab07f246666e258fc434d3643
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zcloudjp.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Zcloudjp
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'zcloudjp'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install zcloudjp
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+ require 'ostruct'
3
+ require 'httparty'
4
+ require 'active_support/core_ext'
5
+
6
+ module Zcloudjp
7
+ class Client
8
+ include HTTParty
9
+ format :json
10
+
11
+ attr_reader :base_uri, :api_key
12
+
13
+ def initialize(options={})
14
+ @api_key = options.delete(:api_key) || ENV['ZCLOUDJP_API_KEY']
15
+ @base_uri = options[:endpoint] || "https://my.z-cloud.jp"
16
+ unless @api_key; raise ArgumentError, "options[:api_key] required"; end
17
+ request_options
18
+ end
19
+
20
+ def request_options
21
+ @request_options = {
22
+ :base_uri => @base_uri,
23
+ :headers => {
24
+ "X-API-KEY" => @api_key,
25
+ "Content-Type" => "application/json; charset=utf-8",
26
+ "Accept" => "application/json",
27
+ "User-Agent" => Zcloudjp::USER_AGENT
28
+ }
29
+ }
30
+ end
31
+
32
+ # Defines the method if not defined yet.
33
+ def method_missing(method, *args, &block)
34
+ self.class.class_eval do
35
+ attr_accessor method.to_sym
36
+
37
+ # Defined a method according to the given method name
38
+ define_method method.to_sym do
39
+ obj = OpenStruct.new(request_options: @request_options)
40
+ obj.extend Zcloudjp.const_get(method.capitalize.to_sym)
41
+ instance_variable_set(:"@#{method}", obj)
42
+ end
43
+ end
44
+
45
+ # Sends to the now-defined method.
46
+ self.__send__(method.to_sym)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,68 @@
1
+ # encoding: utf-8
2
+
3
+ module Zcloudjp
4
+ module Machine
5
+
6
+ # GET /machines.json
7
+ def index(params={})
8
+ self.request_options = self.request_options.merge(params[:query] || {})
9
+ response = Zcloudjp::Client.get("/machines", self.request_options)
10
+ response.parsed_response.map do |res|
11
+ update_attributes(res).clone
12
+ end
13
+ end
14
+ alias :list :index
15
+
16
+ # GET /machines/:id.json
17
+ def show(params={})
18
+ id = params.delete(:id) || self.id
19
+ response = Zcloudjp::Client.get("/machines/#{id}", self.request_options)
20
+ update_attributes(response.parsed_response)
21
+ end
22
+ alias :find_by :show
23
+
24
+ # POST /machines.json
25
+ def create(params={})
26
+ self.request_options = self.request_options.merge(body: params.to_json)
27
+ response = Zcloudjp::Client.post("/machines", self.request_options)
28
+ update_attributes(response.parsed_response)
29
+ end
30
+
31
+ # DELETE /machines/:id.json
32
+ def delete(params={})
33
+ id = params.delete(:id) || self.id
34
+ response = Zcloudjp::Client.delete("/machines/#{id}", self.request_options)
35
+ update_attributes(response.parsed_response)
36
+ end
37
+
38
+ # POST /machines/:id/start.json
39
+ def start(params={})
40
+ id = params.delete(:id) || self.id
41
+ response = Zcloudjp::Client.post("/machines/#{id}/start", self.request_options)
42
+ update_attributes(response.parsed_response)
43
+ end
44
+
45
+ # POST /machines/:id/stop.json
46
+ def stop(params={})
47
+ id = params.delete(:id) || self.id
48
+ response = Zcloudjp::Client.post("/machines/#{id}/stop", self.request_options)
49
+ update_attributes(response.parsed_response)
50
+ end
51
+
52
+ # Create a Metadata object related to the specified machine
53
+ def metadata
54
+ Metadata.new(self)
55
+ end
56
+
57
+ protected
58
+
59
+ def update_attributes(response)
60
+ response = response.first if response.is_a? Array
61
+ response.each do |key, value|
62
+ self.instance_variable_set(:"@#{key}", value)
63
+ self.send("#{key}=", value)
64
+ end
65
+ self
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+ require 'zcloudjp/utils'
3
+
4
+ module Zcloudjp
5
+ class Metadata
6
+ include Utils
7
+
8
+ attr_reader :machine
9
+
10
+ # TODO
11
+ # Should be hybrid: executable when a machine object is given or not.
12
+
13
+ def initialize(machine)
14
+ @machine = machine
15
+ end
16
+
17
+ # GET /machines/:id/metadata.:format
18
+ def index
19
+ Zcloudjp::Client.get("/machines/#{machine.id}/metadata", machine.request_options)
20
+ end
21
+ alias :list :index
22
+
23
+ # GET /machines/:id/metadata/:key.:format
24
+ def show(params={})
25
+ key = params.delete(:key)
26
+ Zcloudjp::Client.get("/machines/#{machine.id}/metadata/#{key}", machine.request_options)
27
+ end
28
+ alias :find_by :show
29
+
30
+ # PUT /machines/:id/metadata.:format
31
+ def create(params={})
32
+ key = params.delete(:key)
33
+ machine.request_options = machine.request_options.merge(body: parse_params(params, :metadata).to_json)
34
+ Zcloudjp::Client.put("/machines/#{machine.id}/metadata/#{key}", machine.request_options)
35
+ end
36
+
37
+ # PUT /machines/:id/metadata/:key.:format
38
+ def update(params={})
39
+ machine.request_options = machine.request_options.merge(body: parse_params(params, :metadata).to_json)
40
+ Zcloudjp::Client.put("/machines/#{machine.id}/metadata", machine.request_options)
41
+ end
42
+
43
+ # DELETE /machines/:id/metadata/:key.:format
44
+ def delete(params={})
45
+ key = params.delete(:key)
46
+ Zcloudjp::Client.delete("/machines/#{machine.id}/metadata/#{key}", machine.request_options)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ module Zcloudjp
4
+ module Utils
5
+
6
+ # Parses given params or file and returns Hash including the given key.
7
+ def parse_params(params, key_word)
8
+ body = params.has_key?(:path) ? load_file(params[:path], key_word) : params
9
+ body = { key_word => body } unless body.has_key?(key_word.to_sym)
10
+ body
11
+ end
12
+
13
+ # Loads a specified file and returns Hash including the given key.
14
+ def load_file(path, key_word)
15
+ begin
16
+ data = MultiJson.load(IO.read(File.expand_path(path)), symbolize_keys: true)
17
+ rescue RuntimeError, Errno::ENOENT => e
18
+ raise e.message
19
+ rescue MultiJson::LoadError => e
20
+ raise e.message
21
+ end
22
+ if data.has_key?(key_word)
23
+ data[key_word].map { |k,v| data[key_word][k] = v } if data[key_word].is_a? Hash
24
+ end
25
+ data
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module Zcloudjp
2
+ VERSION = "0.1.0"
3
+ end
data/lib/zcloudjp.rb ADDED
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ require 'zcloudjp/version'
3
+ require 'zcloudjp/client'
4
+ require 'zcloudjp/machine'
5
+ require 'zcloudjp/metadata'
6
+ require 'Zcloudjp/utils'
7
+
8
+ module Zcloudjp
9
+ USER_AGENT = "zcloudjp-gem/#{Zcloudjp::VERSION} (#{RUBY_PLATFORM}) ruby/#{RUBY_VERSION}"
10
+
11
+ class << self
12
+ def new(options)
13
+ @client = Zcloudjp::Client.new(options)
14
+ @client
15
+ end
16
+
17
+ def user_agent
18
+ @@user_agent ||= USER_AGENT
19
+ end
20
+
21
+ def user_agent=(agent)
22
+ @@user_agent = agent
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1 @@
1
+ { "metadata": { "name": "foo" } }
@@ -0,0 +1,6 @@
1
+ {
2
+ "metadata": {
3
+ "user-data": "{\"name\":\"foo\",\"email\":\"foo@examle.com\"}"
4
+ }
5
+ }
6
+
@@ -0,0 +1,21 @@
1
+ {
2
+ "id":"1",
3
+ "name":"my_smart_machine",
4
+ "type":"smartmachine",
5
+ "state":"stopped",
6
+ "dataset":"sdc:sdc:base64:1.8.4",
7
+ "memory":1024,
8
+ "disk":30720,
9
+ "ips":[
10
+ "192.168.1.10"
11
+ ],
12
+ "metadata":{},
13
+ "created":"2013-01-25T04:57:30+00:00",
14
+ "updated":"2013-02-07T06:32:41+00:00",
15
+ "account_id":723,
16
+ "os":"SmartOS",
17
+ "kind":"SmartOS",
18
+ "package":"Small_1GB",
19
+ "subdomain":null,
20
+ "alert":true
21
+ }
@@ -0,0 +1 @@
1
+ {"dataset":"sdc:sdc:base64:1.8.4","metadata":{"user-script":"#!/bin/bash\n\nexists() {\n if command -v $1 \u0026\u003E/dev/null\n then\n return 0\n else\n return 1\n fi\n}\n\nif ! exists /opt/local/bin/chef-client; then\n pkgin -y install gcc47 gcc47-runtime scmgit-base scmgit-docs gmake ruby193-base ruby193-yajl ruby193-nokogiri ruby193-readline pkg-config\n\n ## for smf cookbook\n pkgin -y install libxslt\n\n ## install chef\n gem update --system\n gem install --no-ri --no-rdoc ohai\n gem install --no-ri --no-rdoc json\n gem install --no-ri --no-rdoc chef --version=10.20.0\n gem install --no-ri --no-rdoc rb-readline\nfi\n\nmkdir -p /etc/chef /var/log/chef\n","user-data":"{ \"name\": \"foo\", \"email\": \"foo@examle.com\" }"},"name":"my_smart_machine","package":"Small_1GB"}
@@ -0,0 +1,44 @@
1
+ [
2
+ {
3
+ "id": "123",
4
+ "name": "SmartOS",
5
+ "type": "smartmachine",
6
+ "state": "running",
7
+ "dataset": "sdc:sdc:base64:1.8.4",
8
+ "memory": 1024,
9
+ "disk": 30720,
10
+ "ips": [
11
+ "192.168.0.1"
12
+ ],
13
+ "metadata": {},
14
+ "created": "2013-01-25T04:57:30+00:00",
15
+ "updated": "2013-02-11T03:10:10+00:00",
16
+ "account_id": 723,
17
+ "os": "SmartOS",
18
+ "kind": "SmartOS",
19
+ "package": "Small_1GB",
20
+ "subdomain": null,
21
+ "alert": true
22
+ },
23
+ {
24
+ "id": "456",
25
+ "name": null,
26
+ "type": "smartmachine",
27
+ "state": "running",
28
+ "dataset": "sdc:sdc:base64:1.8.4",
29
+ "memory": 1024,
30
+ "disk": 30720,
31
+ "ips": [
32
+ "192.168.0.2"
33
+ ],
34
+ "metadata": {},
35
+ "created": "2013-02-01T02:36:32+00:00",
36
+ "updated": "2013-02-12T03:40:23+00:00",
37
+ "account_id": 723,
38
+ "os": "SmartOS",
39
+ "kind": "SmartOS",
40
+ "package": "Small_1GB",
41
+ "subdomain": null,
42
+ "alert": false
43
+ }
44
+ ]
@@ -0,0 +1,7 @@
1
+ {
2
+ "metadata": {
3
+ "user-script": "#!/bin/bash\n\nexists() {\n if command -v $1 &>/dev/null\n then\n return 0\n else\n return 1\n fi\n}\n\nif ! exists /opt/local/bin/chef-client; then\n pkgin -y install gcc47 gcc47-runtime scmgit-base scmgit-docs gmake ruby193-base ruby193-yajl ruby193-nokogiri ruby193-readline pkg-config\n\n ## for smf cookbook\n pkgin -y install libxslt\n\n ## install chef\n gem update --system\n gem install --no-ri --no-rdoc ohai\n gem install --no-ri --no-rdoc json\n gem install --no-ri --no-rdoc chef --version=10.20.0\n gem install --no-ri --no-rdoc rb-readline\nfi\n\nmkdir -p /etc/chef /var/log/chef\n",
4
+ "user-data": "{\"name\":\"foo\",\"email\":\"foo@examle.com\"}",
5
+ "user-name": "foo"
6
+ }
7
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "value": "{\"name\":\"foo\",\"email\":\"foo@example.com\"}"
3
+ }
@@ -0,0 +1 @@
1
+ {"metadata":{"user-name":"awesome"}}
@@ -0,0 +1,7 @@
1
+ {
2
+ "metadata": {
3
+ "user-script": "#!/bin/bash\n\nexists() {\n if command -v $1 &>/dev/null\n then\n return 0\n else\n return 1\n fi\n}\n\nif ! exists /opt/local/bin/chef-client; then\n pkgin -y install gcc47 gcc47-runtime scmgit-base scmgit-docs gmake ruby193-base ruby193-yajl ruby193-nokogiri ruby193-readline pkg-config\n\n ## for smf cookbook\n pkgin -y install libxslt\n\n ## install chef\n gem update --system\n gem install --no-ri --no-rdoc ohai\n gem install --no-ri --no-rdoc json\n gem install --no-ri --no-rdoc chef --version=10.20.0\n gem install --no-ri --no-rdoc rb-readline\nfi\n\nmkdir -p /etc/chef /var/log/chef\n",
4
+ "user-data": "{\"name\":\"foo\",\"email\":\"foo@examle.com\"}",
5
+ "user-name": "awesome"
6
+ }
7
+ }
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+
3
+ unless ENV['CI']
4
+ require 'simplecov'
5
+ SimpleCov.start do
6
+ add_filter 'spec'
7
+ end
8
+ end
9
+
10
+ require 'zcloudjp'
11
+ require 'rspec'
12
+ require 'multi_json'
13
+ require 'webmock/rspec'
14
+
15
+ RSpec.configure do |config|
16
+ config.expect_with :rspec do |c|
17
+ c.syntax = :expect
18
+ end
19
+ end
20
+
21
+ def stub_delete(endpoint, path)
22
+ stub_request(:delete, endpoint + path)
23
+ end
24
+
25
+ def stub_get(endpoint, path)
26
+ stub_request(:get, endpoint + path)
27
+ end
28
+
29
+ def stub_post(endpoint, path)
30
+ stub_request(:post, endpoint + path)
31
+ end
32
+
33
+ def stub_put(endpoint, path)
34
+ stub_request(:put, endpoint + path)
35
+ end
36
+
37
+ def fixture_path
38
+ File.expand_path("../fixtures", __FILE__)
39
+ end
40
+
41
+ def fixture(file)
42
+ File.new(fixture_path + '/' + file)
43
+ end
@@ -0,0 +1,152 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Zcloudjp::Machine do
5
+ before do
6
+ @client = Zcloudjp.new(
7
+ api_key: "abc",
8
+ base_uri: "https://my.z-cloud.jp"
9
+ )
10
+ @machine_methods = Zcloudjp::Machine.public_instance_methods
11
+ end
12
+
13
+ describe "#index" do
14
+ before do
15
+ stub_get(@client.base_uri, "/machines").to_return(
16
+ :status => 200, :body => fixture("machines.json"), :headers => { :content_type => "application/json; charset=utf-8" }
17
+ )
18
+ end
19
+
20
+ it "returns an array having 2 object" do
21
+ machines = @client.machine.list
22
+ expect(machines.size).to eql 2
23
+ end
24
+
25
+ it "returns an open_struct object" do
26
+ machines = @client.machine.list
27
+ expect(machines.first).to be_a OpenStruct
28
+ end
29
+
30
+ it "returns true when the object is extended" do
31
+ machines = @client.machine.list
32
+ @machine_methods.each { |method| expect(machines.first).to respond_to method }
33
+ end
34
+ end
35
+
36
+ describe "#show" do
37
+ before do
38
+ stub_get(@client.base_uri, "/machines/1").to_return(
39
+ :status => 200, :body => fixture("machine.json"), :headers => {:content_type => "application/json; charset=utf-8"}
40
+ )
41
+ end
42
+
43
+ it "returns an open_struct object" do
44
+ machine = @client.machine.find_by id: '1'
45
+ expect(machine).to be_a OpenStruct
46
+ end
47
+
48
+ it "returns an id that is equal to the given id" do
49
+ machine = @client.machine.find_by id: '1'
50
+ expect(machine.id).to eq '1'
51
+ end
52
+
53
+ it "returns true when the object is extended" do
54
+ machine = @client.machine.find_by id: '1'
55
+ @machine_methods.each do |method|
56
+ expect(machine).to respond_to method
57
+ end
58
+ end
59
+ end
60
+
61
+ describe "#create" do
62
+ before do
63
+ stub_post(@client.base_uri, "/machines").with(:body => fixture('machine_with_metadata.json').read.chomp).to_return(
64
+ :status => 201, :body => fixture("machine.json"), :headers => {:content_type => "application/json; charset=utf-8"}
65
+ )
66
+ end
67
+
68
+ it "returns true when the machine name is equal to the given name" do
69
+ machine = @client.machine.create(MultiJson.load(fixture('machine_with_metadata.json'), symbolize_keys: true))
70
+ expect(machine.name).to eq "my_smart_machine"
71
+ end
72
+
73
+ it "returns true when the object is extended" do
74
+ machine = @client.machine.create(MultiJson.load(fixture('machine_with_metadata.json'), symbolize_keys: true))
75
+ @machine_methods.each do |method|
76
+ expect(machine).to respond_to method
77
+ end
78
+ end
79
+ end
80
+
81
+ describe "#delete" do
82
+ before do
83
+ stub_delete(@client.base_uri, "/machines/1").
84
+ to_return(:status => 200, :body => fixture("machine.json"), :headers => {:content_type => "application/json; charset=utf-8"})
85
+ end
86
+
87
+ it "returns OpenStruct with machine attributes" do
88
+ machine = @client.machine.delete(id: '1')
89
+ expect(machine).to be_a OpenStruct
90
+ end
91
+
92
+ it "returns an id that is equal to the given id" do
93
+ machine = @client.machine.delete(id: '1')
94
+ expect(machine.id).to eql '1'
95
+ end
96
+
97
+ it "returns true if the methods are extended" do
98
+ machine = @client.machine.delete(id: '1')
99
+ @machine_methods.each do |method|
100
+ expect(machine).to respond_to method
101
+ end
102
+ end
103
+ end
104
+
105
+ describe "#start" do
106
+ before do
107
+ stub_post(@client.base_uri, "/machines/1/start").
108
+ to_return(:status => 201, :body => fixture("machine.json"), :headers => {:content_type => "application/json; charset=utf-8"})
109
+ end
110
+
111
+ it "returns OpenStruct with machine attributes" do
112
+ machine = @client.machine.start(id: '1')
113
+ expect(machine).to be_a OpenStruct
114
+ end
115
+
116
+ it "returns an id that is equal to the given id" do
117
+ machine = @client.machine.start(id: '1')
118
+ expect(machine.id).to eq '1'
119
+ end
120
+
121
+ it "returns true if the object is extended" do
122
+ machine = @client.machine.start(id: '1')
123
+ @machine_methods.each do |method|
124
+ expect(machine).to respond_to method
125
+ end
126
+ end
127
+ end
128
+
129
+ describe "#stop" do
130
+ before do
131
+ stub_post(@client.base_uri, "/machines/1/stop").
132
+ to_return(:status => 201, :body => fixture("machine.json"), :headers => {:content_type => "application/json; charset=utf-8"})
133
+ end
134
+
135
+ it "returns OpenStruct with machine attributes" do
136
+ machine = @client.machine.stop(id: '1')
137
+ expect(machine).to be_a OpenStruct
138
+ end
139
+
140
+ it "returns an id that is equal to the given id" do
141
+ machine = @client.machine.stop(id: '1')
142
+ expect(machine.id).to eq '1'
143
+ end
144
+
145
+ it "returns true if the object is extended" do
146
+ machine = @client.machine.stop(id: '1')
147
+ @machine_methods.each do |method|
148
+ expect(machine).to respond_to method
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,112 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Zcloudjp::Metadata do
5
+ before do
6
+ @client = Zcloudjp.new(
7
+ api_key: "abc",
8
+ base_uri: "https://my.z-cloud.jp"
9
+ )
10
+ stub_get(@client.base_uri, "/machines/1").
11
+ to_return(:status => 200, :body => fixture("machine.json"), :headers => {:content_type => "application/json; charset=utf-8"})
12
+ end
13
+
14
+ describe "#index" do
15
+ before do
16
+ stub_get(@client.base_uri, "/machines/1/metadata").
17
+ to_return(:status => 200, :body => fixture("metadata.json"), :headers => {:content_type => "application/json; charset=utf-8"})
18
+ end
19
+
20
+ it "returns true when having the user-script key" do
21
+ machine = @client.machine.find_by id: "1"
22
+ metadata = machine.metadata.list
23
+ expect(metadata.parsed_response["metadata"].has_key?("user-script")).to be_true
24
+ end
25
+
26
+ it "returns true when having the user-data key" do
27
+ machine = @client.machine.find_by id: "1"
28
+ metadata = machine.metadata.list
29
+ expect(metadata.parsed_response["metadata"].has_key?("user-data")).to be_true
30
+ end
31
+ end
32
+
33
+ describe "#show" do
34
+ before do
35
+ stub_get(@client.base_uri, "/machines/1/metadata/user-data").
36
+ to_return(:status => 200, :body => fixture("metadata_user-data.json"), :headers => {:content_type => "application/json; charset=utf-8"})
37
+ end
38
+
39
+ it "returns true when having a value for the user-script key" do
40
+ machine = @client.machine.find_by id: "1"
41
+ metadata = machine.metadata.find_by key: "user-data"
42
+ expect(metadata.parsed_response["value"]).to match(/foo@example\.com/)
43
+ end
44
+
45
+ it "returns true when Hash has a value for user-data key" do
46
+ machine = @client.machine.find_by id: "1"
47
+ metadata = machine.metadata.find_by key: "user-data"
48
+ expect(metadata.parsed_response["value"]).to match(/foo@example\.com/)
49
+ end
50
+ end
51
+
52
+ describe "#create" do
53
+ before do
54
+ stub_put(@client.base_uri, "/machines/1/metadata/user-name").
55
+ with(:body => MultiJson.load(fixture("add_a_key_into_metadata.json"))).
56
+ to_return(:status => 200, :body => fixture("metadata.json"), :headers => {:content_type => "application/json; charset=utf-8"})
57
+ end
58
+
59
+ context "params given by Hash object" do
60
+ before do
61
+ machine = @client.machine.find_by id: "1"
62
+ @metadata = machine.metadata.create(
63
+ MultiJson.load(fixture('add_a_key_into_metadata.json'), symbolize_keys: true).merge!(key: 'user-name')
64
+ )
65
+ end
66
+
67
+ it "returns true when having a value for the user-script key" do
68
+ expect(@metadata.parsed_response["metadata"]["user-script"]).to match(/if command/)
69
+ end
70
+
71
+ it "returns true when having a value for the user-data key" do
72
+ expect(@metadata.parsed_response["metadata"]["user-data"]).to match(/foo@examle\.com/)
73
+ end
74
+
75
+ it "returns true when having a value for the user-name key" do
76
+ expect(@metadata.parsed_response["metadata"]["user-name"]).to match(/foo/)
77
+ end
78
+ end
79
+ end
80
+
81
+ describe "#update" do
82
+ before do
83
+ stub_put(@client.base_uri, "/machines/1/metadata").
84
+ with(:body => MultiJson.load(fixture("update_a_key_of_metadata.json"))).
85
+ to_return(:status => 200, :body => fixture("updated_metadata.json"), :headers => {:content_type => "application/json; charset=utf-8"})
86
+ end
87
+
88
+ context "params given by Hash object" do
89
+ before do
90
+ machine = @client.machine.find_by id: "1"
91
+ @metadata = machine.metadata.update({"user-name"=>"awesome"})
92
+ end
93
+
94
+ it "returns true when having value for the user-script key" do
95
+ expect(@metadata.parsed_response["metadata"]["user-name"]).to match(/awesome/)
96
+ end
97
+ end
98
+ end
99
+
100
+ describe "#delete" do
101
+ before do
102
+ stub_delete(@client.base_uri, "/machines/1/metadata/user-data").
103
+ to_return(:status => 200, :body => fixture("delete_metadata.json"), :headers => {:content_type => "application/json; charset=utf-8"})
104
+ end
105
+
106
+ it "returns true when a specified metadata is deleted" do
107
+ machine = @client.machine.find_by id: "1"
108
+ @metadata = machine.metadata.delete(key: 'user-data')
109
+ expect(@metadata.parsed_response).to eq MultiJson.load(fixture('delete_metadata.json'))
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Zcloudjp do
5
+ describe ".new" do
6
+ it "creates a client" do
7
+ client = Zcloudjp.new(:api_key => 'abc')
8
+ expect(client).to be_a Zcloudjp::Client
9
+ end
10
+
11
+ it "raises an ArgumentError when the API key is not specified" do
12
+ expect{ Zcloudjp.new }.to raise_error ArgumentError
13
+ end
14
+ end
15
+ end
data/zcloudjp.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zcloudjp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zcloudjp"
8
+ spec.version = Zcloudjp::VERSION
9
+ spec.authors = ["azukiwasher"]
10
+ spec.email = ["azukiwasher@higanworks.com"]
11
+ spec.description = %q{A Ruby interface to the Z Cloud API}
12
+ spec.summary = %q{A Ruby interface to the Z Cloud API.}
13
+ spec.homepage = "https://github.com/giraffi/zcloudjp"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_runtime_dependency 'activesupport'
25
+ spec.add_runtime_dependency 'httparty'
26
+ spec.add_runtime_dependency 'multi_json'
27
+
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'simplecov'
30
+ spec.add_development_dependency 'webmock'
31
+ end
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zcloudjp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - azukiwasher
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: multi_json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: A Ruby interface to the Z Cloud API
126
+ email:
127
+ - azukiwasher@higanworks.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .gitignore
133
+ - Gemfile
134
+ - LICENSE.txt
135
+ - README.md
136
+ - Rakefile
137
+ - lib/zcloudjp.rb
138
+ - lib/zcloudjp/client.rb
139
+ - lib/zcloudjp/machine.rb
140
+ - lib/zcloudjp/metadata.rb
141
+ - lib/zcloudjp/utils.rb
142
+ - lib/zcloudjp/version.rb
143
+ - spec/fixtures/add_a_key_into_metadata.json
144
+ - spec/fixtures/delete_metadata.json
145
+ - spec/fixtures/machine.json
146
+ - spec/fixtures/machine_with_metadata.json
147
+ - spec/fixtures/machines.json
148
+ - spec/fixtures/metadata.json
149
+ - spec/fixtures/metadata_user-data.json
150
+ - spec/fixtures/update_a_key_of_metadata.json
151
+ - spec/fixtures/updated_metadata.json
152
+ - spec/spec_helper.rb
153
+ - spec/zcloudjp/machine_spec.rb
154
+ - spec/zcloudjp/metadata_spec.rb
155
+ - spec/zcloudjp_spec.rb
156
+ - zcloudjp.gemspec
157
+ homepage: https://github.com/giraffi/zcloudjp
158
+ licenses:
159
+ - MIT
160
+ metadata: {}
161
+ post_install_message:
162
+ rdoc_options: []
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - '>='
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - '>='
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ requirements: []
176
+ rubyforge_project:
177
+ rubygems_version: 2.0.3
178
+ signing_key:
179
+ specification_version: 4
180
+ summary: A Ruby interface to the Z Cloud API.
181
+ test_files:
182
+ - spec/fixtures/add_a_key_into_metadata.json
183
+ - spec/fixtures/delete_metadata.json
184
+ - spec/fixtures/machine.json
185
+ - spec/fixtures/machine_with_metadata.json
186
+ - spec/fixtures/machines.json
187
+ - spec/fixtures/metadata.json
188
+ - spec/fixtures/metadata_user-data.json
189
+ - spec/fixtures/update_a_key_of_metadata.json
190
+ - spec/fixtures/updated_metadata.json
191
+ - spec/spec_helper.rb
192
+ - spec/zcloudjp/machine_spec.rb
193
+ - spec/zcloudjp/metadata_spec.rb
194
+ - spec/zcloudjp_spec.rb
195
+ has_rdoc: