├── .gitignore ├── .gitmodules ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── CONTRIBUTORS.md ├── Gemfile ├── LICENSE.md ├── README.md ├── Rakefile ├── fog-sakuracloud.gemspec ├── lib └── fog │ ├── bin │ └── sakuracloud.rb │ ├── sakuracloud.rb │ └── sakuracloud │ ├── compute.rb │ ├── dns.rb │ ├── models │ ├── compute │ │ ├── plan.rb │ │ ├── plans.rb │ │ ├── server.rb │ │ ├── servers.rb │ │ ├── ssh_key.rb │ │ ├── ssh_keys.rb │ │ ├── zone.rb │ │ └── zones.rb │ ├── dns │ │ ├── zone.rb │ │ └── zones.rb │ ├── network │ │ ├── interface.rb │ │ ├── interfaces.rb │ │ ├── router.rb │ │ ├── routers.rb │ │ ├── switch.rb │ │ └── switches.rb │ ├── script │ │ ├── note.rb │ │ └── notes.rb │ └── volume │ │ ├── archive.rb │ │ ├── archives.rb │ │ ├── disk.rb │ │ ├── disks.rb │ │ ├── plan.rb │ │ └── plans.rb │ ├── network.rb │ ├── requests │ ├── compute │ │ ├── boot_server.rb │ │ ├── create_server.rb │ │ ├── delete_server.rb │ │ ├── list_plans.rb │ │ ├── list_servers.rb │ │ ├── list_ssh_keys.rb │ │ ├── list_zones.rb │ │ └── stop_server.rb │ ├── dns │ │ ├── create_zone.rb │ │ ├── delete_zone.rb │ │ ├── list_zones.rb │ │ └── modify_zone.rb │ ├── network │ │ ├── change_router_bandwidth.rb │ │ ├── collect_monitor_router.rb │ │ ├── connect_interface_to_switch.rb │ │ ├── create_router.rb │ │ ├── create_switch.rb │ │ ├── delete_interface.rb │ │ ├── delete_router.rb │ │ ├── delete_switch.rb │ │ ├── list_interfaces.rb │ │ ├── list_routers.rb │ │ ├── list_switches.rb │ │ └── regist_interface_to_server.rb │ ├── script │ │ ├── create_note.rb │ │ ├── delete_note.rb │ │ ├── list_notes.rb │ │ └── modify_note.rb │ └── volume │ │ ├── associate_ip_to_disk.rb │ │ ├── attach_disk.rb │ │ ├── carve_hostname_on_disk.rb │ │ ├── configure_disk.rb │ │ ├── create_disk.rb │ │ ├── delete_disk.rb │ │ ├── list_archives.rb │ │ ├── list_disks.rb │ │ ├── list_plans.rb │ │ └── register_note_to_disk.rb │ ├── script.rb │ ├── utils │ └── request.rb │ ├── version.rb │ └── volume.rb └── tests ├── helper.rb └── sakuracloud └── requests ├── compute ├── plans_tests.rb ├── servers_tests.rb ├── ssh_keys_tests.rb └── zones_tests.rb ├── network ├── routers_tests.rb └── switches_tests.rb └── volume ├── archives_tests.rb ├── disks_tests.rb └── plans_tests.rb /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | *.bundle 11 | *.so 12 | *.o 13 | *.a 14 | mkmf.log 15 | # Created by http://www.gitignore.io 16 | 17 | ### Ruby ### 18 | *.gem 19 | *.rbc 20 | /.config 21 | /coverage/ 22 | /InstalledFiles 23 | /pkg/ 24 | /spec/reports/ 25 | /test/tmp/ 26 | /test/version_tmp/ 27 | /tmp/ 28 | 29 | ## Specific to RubyMotion: 30 | .dat* 31 | .repl_history 32 | build/ 33 | 34 | ## Documentation cache and generated files: 35 | /.yardoc/ 36 | /_yardoc/ 37 | /doc/ 38 | /rdoc/ 39 | 40 | ## Environment normalisation: 41 | /.bundle/ 42 | /lib/bundler/man/ 43 | 44 | # for a library or gem, you might want to ignore these files since the code is 45 | # intended to run in multiple environments; otherwise, check them in: 46 | # Gemfile.lock 47 | # .ruby-version 48 | # .ruby-gemset 49 | 50 | # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: 51 | .rvmrc 52 | 53 | 54 | ### vim ### 55 | [._]*.s[a-w][a-z] 56 | [._]s[a-w][a-z] 57 | *.un~ 58 | Session.vim 59 | .netrwhist 60 | *~ 61 | 62 | .fog 63 | /vendor/ 64 | /bbin/ 65 | 66 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fog/fog-sakuracloud/bedf32c200cb74ce453c8f7b299b54188dd81504/.gitmodules -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | 3 | script: bundle exec rake travis 4 | 5 | rvm: 6 | - 2.2 7 | - 2.1 8 | - 2.0.0 9 | gemfile: 10 | - Gemfile 11 | before_install: 12 | - gem update bundler 13 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog of fog-sakuracloud 2 | 3 | ## v1.7.5 4 | 5 | - Bug: Server#create_and_attach_volume ignores api_zone. => TODO: Cleanup 6 | 7 | ## v1.7.4 8 | 9 | - Bug: forgot zone is1a... 10 | 11 | ## v1.7.3 12 | 13 | - Coordinate: Router#change_bandwidth update current instance by new ID. 14 | 15 | ## v1.7.2 16 | 17 | - Bug: remove debug code from create_router 18 | 19 | ## v1.7.1 20 | 21 | - Misc: Pass zone from Fog.credentials[:sakuracloud_api_zone]. 22 | 23 | ## v1.7.0 24 | 25 | - Change: Rebuild router model from internet resource. 26 | 27 | ## v1.6.0 28 | 29 | - Miscs: Wrap request for error messages to be readable. #22 30 | - Cleanup: replace @attribute[:id] to id #21 31 | 32 | ## v1.5.2 33 | 34 | - Feature: Add filter params Start and End to collect_monitor_router. 35 | 36 | ## v1.5.1 37 | 38 | - Feature: Add monitor to Router model PR #20 HT: @miyukki 39 | 40 | ## v1.5.0 41 | 42 | - Feature: # Add new option api_zone to Provider to select zone easily. 43 | 44 | ## v1.4.0 45 | 46 | - Feature: #17 Support Standard Disk Plan for Disk creation HT: @noralife 47 | - Feature: #18 Add attach feature to Disk Model HT: @noralife 48 | 49 | ## v1.3.3 50 | 51 | - Typo: Fix typo ( auth_encord -> auth_encode ) #16 HT: @mazgi 52 | - Drop support ruby 1.9, add 2.2 53 | 54 | ## v1.3.2 55 | 56 | - Feature: Set hostname (Disk#carve_hostname_on_disk) 57 | 58 | ## v1.3.1 59 | 60 | - Feature: Add simple setter to ResourceRecordSets 61 | 62 | ## v1.3.0 63 | 64 | - Feature: Manage Cloud DNS 65 | 66 | ## v1.2.0 67 | 68 | - Feature: Manage Interface. 69 | 70 | ## v1.1.1 71 | 72 | - Bugfix: Delete Server 73 | 74 | ## v1.1.0 75 | 76 | - Feature: Support Install Script. 77 | 78 | ## v1.0.1 79 | 80 | - #11 Using Relative paths. 81 | 82 | ## v0.1.2 83 | 84 | - Clean up: #7 HT: @starbelly 85 | 86 | ## v0.1.1 87 | 88 | - Add: Associate IP address to disk 89 | 90 | ## v0.1.0 91 | 92 | - Add: Network resources 93 | - switch 94 | - router 95 | - API change: create server requires options type. 96 | 97 | ## v0.0.4 98 | 99 | - move lib/fog/bin/sakuracloud.rb from fog. 100 | 101 | ## v0.0.1-0.0.3 102 | 103 | - divide from fog. 104 | - supports 105 | - compute(with public switch only) 106 | - volume 107 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Getting Involved 2 | 3 | New contributors are always welcome, when it doubt please ask questions. We strive to be an open and welcoming community. Please be nice to one another. 4 | 5 | ### Coding 6 | 7 | * Pick a task: 8 | * Offer feedback on open [pull requests](https://github.com/fog/fog-sakuracloud/pulls). 9 | * Review open [issues](https://github.com/fog/fog-sakuracloud/issues) for things to help on. 10 | * [Create an issue](https://github.com/fog/fog-sakuracloud/issues/new) to start a discussion on additions or features. 11 | * Fork the project, add your changes and tests to cover them in a topic branch. 12 | * Commit your changes and rebase against `fog/fog-sakuracloud` to ensure everything is up to date. 13 | * [Submit a pull request](https://github.com/fog/fog-sakuracloud/compare/) 14 | 15 | ### Non-Coding 16 | 17 | * Offer feedback on open [issues](https://github.com/fog/fog-sakuracloud/issues). 18 | * Organize or volunteer at events. -------------------------------------------------------------------------------- /CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | * Bryan Paxton 2 | * Paulo Henrique Lopes Ribeiro 3 | * Yukihiko Sawanobori 4 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in fog-sakuracloud.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2014 [CONTRIBUTORS.md](https://github.com/fog/fog-sakuracloud/blob/master/CONTRIBUTORS.md) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fog::Sakuracloud 2 | 3 | [![Gem Version](https://badge.fury.io/rb/fog-sakuracloud.svg)](http://badge.fury.io/rb/fog-sakuracloud) 4 | [![Build Status](https://travis-ci.org/fog/fog-sakuracloud.svg?branch=master)](https://travis-ci.org/fog/fog-sakuracloud) 5 | [![Stories in Ready](https://badge.waffle.io/fog/fog-sakuracloud.svg?label=ready&title=Ready)](http://waffle.io/fog/fog-sakuracloud) 6 | [![Stories in Progress](https://badge.waffle.io/fog/fog-sakuracloud.svg?label=In%20Progress&title=In%20Progress')](http://waffle.io/fog/fog-sakuracloud) 7 | 8 | This gem is a module for the fog gem that allows you to manage resources in the Sakura no Cloud. 9 | 10 | ## Installation 11 | 12 | Add this line to your application's Gemfile: 13 | 14 | ```ruby 15 | gem 'fog-sakuracloud' 16 | ``` 17 | 18 | And then execute: 19 | 20 | $ bundle 21 | 22 | Or install it yourself as: 23 | 24 | $ gem install fog-sakuracloud 25 | 26 | ## Usage 27 | 28 | See [Getting started](https://github.com/higanworks/fog-sakuracloud/wiki/Getting-started-for-SakuraCloud) 29 | 30 | ## Contributing 31 | 32 | 1. Fork it ( https://github.com/fog/fog-sakuracloud/fork ) 33 | 2. Create your feature branch (`git checkout -b my-new-feature`) 34 | 3. Commit your changes (`git commit -am 'Add some feature'`) 35 | 4. Push to the branch (`git push origin my-new-feature`) 36 | 5. Create a new Pull Request 37 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler/setup' 2 | require "bundler/gem_tasks" 3 | require 'rake/testtask' 4 | require 'date' 5 | require 'rubygems' 6 | require 'rubygems/package_task' 7 | require 'yard' 8 | require File.dirname(__FILE__) + '/lib/fog/sakuracloud' 9 | 10 | ############################################################################# 11 | # 12 | # Helper functions 13 | # 14 | ############################################################################# 15 | 16 | def name 17 | @name ||= Dir['*.gemspec'].first.split('.').first 18 | end 19 | 20 | def version 21 | Fog::VERSION 22 | end 23 | 24 | def date 25 | Date.today.to_s 26 | end 27 | 28 | def rubyforge_project 29 | name 30 | end 31 | 32 | def gemspec_file 33 | "#{name}.gemspec" 34 | end 35 | 36 | def gem_file 37 | "#{name}-#{version}.gem" 38 | end 39 | 40 | def replace_header(head, header_name) 41 | head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"} 42 | end 43 | 44 | ############################################################################# 45 | # 46 | # Standard tasks 47 | # 48 | ############################################################################# 49 | 50 | GEM_NAME = "#{name}" 51 | task :default => :test 52 | task :travis => ['test', 'test:travis'] 53 | 54 | Rake::TestTask.new do |t| 55 | t.pattern = File.join("spec", "**", "*_spec.rb") 56 | end 57 | 58 | namespace :test do 59 | mock = ENV['FOG_MOCK'] || 'true' 60 | task :travis do 61 | sh("export FOG_MOCK=#{mock} && bundle exec shindont") 62 | end 63 | task :vsphere do 64 | sh("export FOG_MOCK=#{mock} && bundle exec shindont tests/vsphere") 65 | end 66 | task :openvz do 67 | sh("export FOG_MOCK=#{mock} && bundle exec shindont tests/openvz") 68 | end 69 | end 70 | 71 | desc 'Run mocked tests for a specific provider' 72 | task :mock, :provider do |t, args| 73 | if args.to_a.size != 1 74 | fail 'USAGE: rake mock[]' 75 | end 76 | provider = args[:provider] 77 | sh("export FOG_MOCK=true && bundle exec shindont tests/#{provider}") 78 | end 79 | 80 | desc 'Run live tests against a specific provider' 81 | task :live, :provider do |t, args| 82 | if args.to_a.size != 1 83 | fail 'USAGE: rake live[]' 84 | end 85 | provider = args[:provider] 86 | sh("export FOG_MOCK=false PROVIDER=#{provider} && bundle exec shindont tests/#{provider}") 87 | end 88 | 89 | task :nuke do 90 | Fog.providers.each do |provider| 91 | next if ['Vmfusion'].include?(provider) 92 | begin 93 | compute = Fog::Compute.new(:provider => provider) 94 | for server in compute.servers 95 | Fog::Formatador.display_line("[#{provider}] destroying server #{server.identity}") 96 | server.destroy rescue nil 97 | end 98 | rescue 99 | end 100 | begin 101 | dns = Fog::DNS.new(:provider => provider) 102 | for zone in dns.zones 103 | for record in zone.records 104 | record.destroy rescue nil 105 | end 106 | Fog::Formatador.display_line("[#{provider}] destroying zone #{zone.identity}") 107 | zone.destroy rescue nil 108 | end 109 | rescue 110 | end 111 | end 112 | end 113 | -------------------------------------------------------------------------------- /fog-sakuracloud.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'fog/sakuracloud/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "fog-sakuracloud" 8 | spec.version = Fog::Sakuracloud::VERSION 9 | spec.authors = ["sawanoboly"] 10 | spec.email = ["sawanoboriyu@higanworks.com"] 11 | spec.summary = %q{Module for the 'fog' gem to support Sakura no Cloud} 12 | spec.description = %q{Module for the 'fog' gem to support Sakura no Cloud} 13 | spec.homepage = "https://github.com/fog/fog-sakuracloud" 14 | spec.license = "MIT" 15 | 16 | spec.files = `git ls-files -z`.split("\x0") 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_dependency "fog-core" 22 | spec.add_dependency "fog-json" 23 | 24 | spec.add_development_dependency "bundler" 25 | ## List your development dependencies here. Development dependencies are 26 | ## those that are only needed during development 27 | spec.add_development_dependency('minitest') 28 | spec.add_development_dependency('rake') 29 | spec.add_development_dependency('rbvmomi2', '~> 3.0') 30 | spec.add_development_dependency('yard') 31 | spec.add_development_dependency('thor') 32 | spec.add_development_dependency('rbovirt', '0.0.24') 33 | spec.add_development_dependency('shindo', '~> 0.3.4') 34 | spec.add_development_dependency('fission') 35 | spec.add_development_dependency('pry') 36 | spec.add_development_dependency('rubocop') if RUBY_VERSION > "1.9" 37 | 38 | end 39 | -------------------------------------------------------------------------------- /lib/fog/bin/sakuracloud.rb: -------------------------------------------------------------------------------- 1 | class SakuraCloud < Fog::Bin 2 | class << self 3 | def class_for(key) 4 | case key 5 | when :compute 6 | Fog::Compute::SakuraCloud 7 | when :volume 8 | Fog::Volume::SakuraCloud 9 | when :network 10 | Fog::Network::SakuraCloud 11 | else 12 | raise ArgumentError, "Unrecognized service: #{key}" 13 | end 14 | end 15 | 16 | def [](service) 17 | @@connections ||= Hash.new do |hash, key| 18 | hash[key] = case key 19 | when :compute 20 | Fog::Logger.warning("SakuraCloud[:compute] is not recommended, use Fog::Compute[:sakuracloud] for portability") 21 | Fog::Compute.new(:provider => 'SakuraCloud') 22 | when :volume 23 | Fog::Logger.warning("SakuraCloud[:volume] is not recommended, use Fog::Volume[:sakuracloud] for portability") 24 | Fog::Volume.new(:provider => 'SakuraCloud') 25 | when :network 26 | Fog::Logger.warning("SakuraCloud[:network] is not recommended, use Fog::Network[:sakuracloud] for portability") 27 | Fog::Network.new(:provider => 'SakuraCloud') 28 | else 29 | raise ArgumentError, "Unrecognized service: #{key.inspect}" 30 | end 31 | end 32 | @@connections[service] 33 | end 34 | 35 | def services 36 | Fog::SakuraCloud.services 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core' 2 | require 'fog/json' 3 | require 'fog/sakuracloud/utils/request' 4 | 5 | module Fog 6 | module Compute 7 | autoload :SakuraCloud, File.expand_path('../sakuracloud/compute', __FILE__) 8 | end 9 | 10 | module Network 11 | autoload :SakuraCloud, File.expand_path('../sakuracloud/network', __FILE__) 12 | end 13 | 14 | module Volume 15 | autoload :SakuraCloud, File.expand_path('../sakuracloud/volume', __FILE__) 16 | end 17 | 18 | module DNS 19 | autoload :SakuraCloud, File.expand_path('../sakuracloud/dns', __FILE__) 20 | end 21 | 22 | module SakuraCloud 23 | extend Fog::Provider 24 | 25 | SAKURACLOUD_API_VERSION = '1.1' unless defined? SAKURACLOUD_API_VERSION 26 | 27 | # Miscs 28 | ## Startup Script 29 | autoload :Script, File.expand_path('../sakuracloud/script', __FILE__) 30 | 31 | service(:compute, 'Compute') 32 | service(:volume, 'Volume') 33 | service(:network, 'Network') 34 | service(:script, 'Script') 35 | service(:dns, 'DNS') 36 | 37 | 38 | def self.api_zones 39 | @api_zones ||= ['tk1a','is1a', 'is1b', 'tk1v'] 40 | end 41 | 42 | def self.validate_api_zone!(api_zone, host=nil) 43 | if !api_zones.include?(api_zone) 44 | raise ArgumentError, "Unknown api_zone: #{api_zone.inspect}" 45 | end 46 | end 47 | 48 | def self.build_endpoint(api_zone) 49 | "/cloud/zone/#{api_zone}/api/cloud/#{SAKURACLOUD_API_VERSION}/" 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/compute.rb: -------------------------------------------------------------------------------- 1 | module Fog 2 | module Compute 3 | class SakuraCloud < Fog::Service 4 | requires :sakuracloud_api_token 5 | requires :sakuracloud_api_token_secret 6 | 7 | recognizes :sakuracloud_api_url, :api_zone 8 | 9 | model_path 'fog/sakuracloud/models/compute' 10 | model :server 11 | collection :servers 12 | model :plan 13 | collection :plans 14 | model :ssh_key 15 | collection :ssh_keys 16 | model :zone 17 | collection :zones 18 | 19 | request_path 'fog/sakuracloud/requests/compute' 20 | request :list_servers 21 | request :create_server 22 | request :delete_server 23 | request :boot_server 24 | request :stop_server 25 | request :list_plans 26 | request :list_ssh_keys 27 | request :list_zones 28 | 29 | class Real 30 | include Fog::SakuraCloud::Utils::Request 31 | 32 | def initialize(options = {}) 33 | @auth_encode = Base64.strict_encode64([ 34 | options[:sakuracloud_api_token], 35 | options[:sakuracloud_api_token_secret] 36 | ].join(':')) 37 | Fog.credentials[:sakuracloud_api_token] = options[:sakuracloud_api_token] 38 | Fog.credentials[:sakuracloud_api_token_secret] = options[:sakuracloud_api_token_secret] 39 | 40 | @sakuracloud_api_url = options[:sakuracloud_api_url] || 'https://secure.sakura.ad.jp' 41 | @api_zone = options[:api_zone] || Fog.credentials[:sakuracloud_api_zone] || 'is1b' 42 | Fog::SakuraCloud.validate_api_zone!(@api_zone) 43 | 44 | @connection = Fog::Core::Connection.new(@sakuracloud_api_url) 45 | end 46 | end 47 | 48 | class Mock 49 | def initialize(options = {}) 50 | end 51 | end 52 | end #SakuraCloud 53 | end #Compute 54 | end 55 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/dns.rb: -------------------------------------------------------------------------------- 1 | module Fog 2 | module DNS 3 | class SakuraCloud < Fog::Service 4 | requires :sakuracloud_api_token 5 | requires :sakuracloud_api_token_secret 6 | 7 | recognizes :sakuracloud_api_url, :api_zone 8 | 9 | model_path 'fog/sakuracloud/models/dns' 10 | model :zone 11 | collection :zones 12 | 13 | request_path 'fog/sakuracloud/requests/dns' 14 | request :list_zones 15 | request :create_zone 16 | request :delete_zone 17 | request :modify_zone 18 | 19 | class Real 20 | include Fog::SakuraCloud::Utils::Request 21 | 22 | def initialize(options = {}) 23 | @auth_encode = Base64.strict_encode64([ 24 | options[:sakuracloud_api_token], 25 | options[:sakuracloud_api_token_secret] 26 | ].join(':')) 27 | Fog.credentials[:sakuracloud_api_token] = options[:sakuracloud_api_token] 28 | Fog.credentials[:sakuracloud_api_token_secret] = options[:sakuracloud_api_token_secret] 29 | 30 | @sakuracloud_api_url = options[:sakuracloud_api_url] || 'https://secure.sakura.ad.jp' 31 | @api_zone = options[:api_zone] || Fog.credentials[:sakuracloud_api_zone] || 'is1b' 32 | Fog::SakuraCloud.validate_api_zone!(@api_zone) 33 | 34 | @connection = Fog::Core::Connection.new(@sakuracloud_api_url) 35 | end 36 | end 37 | 38 | class Mock 39 | def self.data 40 | @data ||= Hash.new do |hash, key| 41 | hash[key] = { 42 | :notes => [] 43 | } 44 | end 45 | end 46 | 47 | def self.reset 48 | @data = nil 49 | end 50 | 51 | def initialize(options={}) 52 | @sakuracloud_api_token = options[:sakuracloud_api_token] 53 | @sakuracloud_api_token_secret = options[:sakuracloud_api_token_secret] 54 | end 55 | 56 | def data 57 | self.class.data[@sakuracloud_api_token] 58 | self.class.data[@sakuracloud_api_token_secret] 59 | end 60 | 61 | def reset_data 62 | self.class.data.delete(@sakuracloud_api_token) 63 | self.class.data.delete(@sakuracloud_api_token_secret) 64 | end 65 | end 66 | end 67 | end 68 | end 69 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/compute/plan.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/model' 2 | 3 | module Fog 4 | module Compute 5 | class SakuraCloud 6 | class Plan < Fog::Model 7 | identity :id, :aliases => 'ID' 8 | attribute :name, :aliases => 'Name' 9 | attribute :server_class, :aliases => 'ServiceClass' 10 | attribute :cpu, :aliases => 'CPU' 11 | attribute :memory_mb, :aliases => 'MemoryMB' 12 | end 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/compute/plans.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/collection' 2 | require 'fog/sakuracloud/models/compute/plan' 3 | 4 | module Fog 5 | module Compute 6 | class SakuraCloud 7 | class Plans < Fog::Collection 8 | model Fog::Compute::SakuraCloud::Plan 9 | 10 | def all 11 | load service.list_plans.body['ServerPlans'] 12 | end 13 | 14 | def get(id) 15 | all.find { |f| f.id == id } 16 | rescue Fog::Errors::NotFound 17 | nil 18 | end 19 | end 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/compute/server.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/model' 2 | 3 | module Fog 4 | module Compute 5 | class SakuraCloud 6 | class Server < Fog::Model 7 | identity :id, :aliases => 'ID' 8 | attribute :name, :aliases => 'Name' 9 | attribute :server_plan, :aliases => 'ServerPlan' 10 | attribute :instance, :aliases => 'Instance' 11 | attribute :disks, :aliases => 'Disks' 12 | attribute :interfaces, :aliases => 'Interfaces' 13 | 14 | def save 15 | requires :name, :server_plan 16 | data = service.create_server(@attributes).body["Server"] 17 | merge_attributes(data) 18 | true 19 | end 20 | 21 | def boot 22 | requires :id 23 | service.boot_server(id) 24 | end 25 | 26 | def stop(force = false) 27 | requires :id 28 | service.stop_server(id, force) 29 | end 30 | 31 | def delete(force = false, disks = []) 32 | requires :id 33 | service.delete_server(id, force, disks) 34 | true 35 | end 36 | alias_method :destroy, :delete 37 | end 38 | end 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/compute/servers.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/collection' 2 | require 'fog/sakuracloud/models/compute/server' 3 | 4 | module Fog 5 | module Compute 6 | class SakuraCloud 7 | class Servers < Fog::Collection 8 | model Fog::Compute::SakuraCloud::Server 9 | 10 | def all 11 | load service.list_servers.body['Servers'] 12 | end 13 | 14 | def get(id) 15 | all.find { |f| f.id == id } 16 | rescue Fog::Errors::NotFound 17 | nil 18 | end 19 | 20 | def regist_interface_to_server(id) 21 | sv = get(id) 22 | sv.regist_interface 23 | sv 24 | end 25 | 26 | def create(options = {}) 27 | user = options[:user] || 'root' 28 | Fog::Logger.warning("Create Server") 29 | name = options[:name] ? options[:name] : Fog::UUID.uuid 30 | data = service.create_server(options).body["Server"] 31 | server = service.servers.new 32 | server.merge_attributes(data) 33 | 34 | if options[:volume] 35 | disk = create_and_attach_volume(server, options) 36 | server.reload 37 | end 38 | server.boot if options[:boot] 39 | server 40 | end 41 | 42 | private 43 | def create_and_attach_volume(server, options) 44 | Fog::Logger.warning("Create Volume") 45 | sakuracloud_api_token = options[:sakuracloud_api_token] || Fog.credentials[:sakuracloud_api_token] 46 | sakuracloud_api_token_secret = options[:sakuracloud_api_token_secret] || Fog.credentials[:sakuracloud_api_token_secret] 47 | api_zone = service.instance_variable_get(:@api_zone) 48 | size_mb = options[:volume][:size_mb].to_i || 20480 49 | volume = Fog::Volume::SakuraCloud.new(:sakuracloud_api_token => sakuracloud_api_token, :sakuracloud_api_token_secret => sakuracloud_api_token_secret, :api_zone => api_zone) 50 | disk = volume.disks.create :name => Fog::UUID.uuid, 51 | :plan => options[:volume][:diskplan].to_i, 52 | :source_archive => options[:volume][:sourcearchive].to_s, 53 | :size_mb => size_mb 54 | Fog::Logger.warning("Waiting disk until available") 55 | disk.wait_for { availability == 'available' } 56 | volume.attach_disk(disk.id, server.id) 57 | disk_attached?(server, disk.id) 58 | Fog::Logger.warning("Modifing disk") 59 | volume.configure_disk(disk.id, options[:sshkey]) 60 | volume.register_note_to_disk(disk.id, options[:startup_script]) if options[:startup_script] 61 | disk 62 | end 63 | 64 | def disk_attached?(server, disk_id) 65 | until server.disks.find {|s| disk_id.to_s} 66 | print '.' 67 | sleep 2 68 | server.reload 69 | end 70 | end 71 | end 72 | end 73 | end 74 | end 75 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/compute/ssh_key.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/model' 2 | 3 | module Fog 4 | module Compute 5 | class SakuraCloud 6 | class SshKey < Fog::Model 7 | identity :id, :aliases => 'ID' 8 | attribute :name, :aliases => 'Name' 9 | attribute :public_key, :aliases => 'PublicKey' 10 | end 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/compute/ssh_keys.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/collection' 2 | require 'fog/sakuracloud/models/compute/ssh_key' 3 | 4 | module Fog 5 | module Compute 6 | class SakuraCloud 7 | class SshKeys < Fog::Collection 8 | model Fog::Compute::SakuraCloud::SshKey 9 | 10 | def all 11 | load service.list_ssh_keys.body['SSHKeys'] 12 | end 13 | 14 | def get(id) 15 | all.find { |f| f.id == id } 16 | rescue Fog::Errors::NotFound 17 | nil 18 | end 19 | end 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/compute/zone.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/model' 2 | 3 | module Fog 4 | module Compute 5 | class SakuraCloud 6 | class Zone < Fog::Model 7 | identity :id, :aliases => 'ID' 8 | attribute :name, :aliases => 'Name' 9 | attribute :description, :aliases => 'Description' 10 | end 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/compute/zones.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/collection' 2 | require 'fog/sakuracloud/models/compute/zone' 3 | 4 | module Fog 5 | module Compute 6 | class SakuraCloud 7 | class Zones < Fog::Collection 8 | model Fog::Compute::SakuraCloud::Zone 9 | 10 | def all 11 | load service.list_zones.body['Zones'] 12 | end 13 | 14 | def get(id) 15 | all.find { |f| f.id == id } 16 | rescue Fog::Errors::NotFound 17 | nil 18 | end 19 | end 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/dns/zone.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/model' 2 | 3 | module Fog 4 | module DNS 5 | class SakuraCloud 6 | class Zone < Fog::Model 7 | identity :id, :aliases => 'ID' 8 | attribute :name, :aliases => 'Name' 9 | attribute :description, :aliases => 'Description' 10 | attribute :status, :aliases => 'Status' 11 | attribute :settings, :aliases => 'Settings' 12 | attribute :tags, :aliases => 'Tags' 13 | 14 | ## Reader methods for nested values. 15 | # Returns value or nil 16 | def rr_sets 17 | settings.fetch('DNS', {}).fetch('ResourceRecordSets', []) if settings 18 | end 19 | 20 | def rr_sets=(rrsets) 21 | raise "ResourceRecordSets must be Array of Hash!" unless rrsets.is_a?(Array) 22 | self.settings = { 23 | 'DNS' => { 24 | 'ResourceRecordSets' => rrsets 25 | } 26 | } 27 | end 28 | 29 | def zone 30 | status.fetch('Zone') if status 31 | end 32 | 33 | def nameservers 34 | status.fetch('NS') if status 35 | end 36 | 37 | def delete 38 | service.delete_zone(identity) 39 | true 40 | end 41 | alias_method :destroy, :delete 42 | 43 | def save 44 | requires :zone 45 | if identity 46 | Fog::Logger.warning("Update DNS Zone #{identity}") 47 | data = service.modify_zone(@attributes).body["CommonServiceItem"] 48 | else 49 | Fog::Logger.warning("Create DNS Zone") 50 | data = service.create_zone(@attributes).body["CommonServiceItem"] 51 | end 52 | merge_attributes(data) 53 | true 54 | end 55 | end 56 | end 57 | end 58 | end 59 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/dns/zones.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/collection' 2 | require 'fog/sakuracloud/models/dns/zone' 3 | 4 | module Fog 5 | module DNS 6 | class SakuraCloud 7 | class Zones < Fog::Collection 8 | model Fog::DNS::SakuraCloud::Zone 9 | 10 | def all 11 | load service.list_zones.body['CommonServiceItems'] 12 | end 13 | 14 | def get(id) 15 | all.find { |f| f.id == id } 16 | rescue Fog::Errors::NotFound 17 | nil 18 | end 19 | 20 | def delete(id) 21 | service.delete_zone(id) 22 | true 23 | end 24 | end 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/network/interface.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/model' 2 | 3 | module Fog 4 | module Network 5 | class SakuraCloud 6 | class Interface < Fog::Model 7 | identity :id, :aliases => 'ID' 8 | attribute :macaddress, :aliases => 'MACAddress' 9 | attribute :ipaddress, :aliases => 'IPAddress' 10 | attribute :user_ipaddress, :aliases => 'UserIPAddress' 11 | attribute :switch, :aliases => 'Switch' 12 | attribute :server, :aliases => 'Server' 13 | 14 | def delete 15 | service.delete_interface(identity) 16 | true 17 | end 18 | alias_method :destroy, :delete 19 | 20 | def connect_to_switch(switch_id) 21 | service.connect_interface_to_switch(identity, switch_id) 22 | true 23 | end 24 | end 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/network/interfaces.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/collection' 2 | require 'fog/sakuracloud/models/network/interface' 3 | 4 | module Fog 5 | module Network 6 | class SakuraCloud 7 | class Interfaces < Fog::Collection 8 | model Fog::Network::SakuraCloud::Interface 9 | 10 | def all 11 | load service.list_interfaces.body['Interfaces'] 12 | end 13 | 14 | def get(id) 15 | all.find { |f| f.id == id } 16 | rescue Fog::Errors::NotFound 17 | nil 18 | end 19 | 20 | def regist_onto_server(server_id) 21 | id = service.regist_interface_to_server(server_id) 22 | get(id) 23 | end 24 | 25 | def connect_to_switch(id, switch_id) 26 | id = service.connect_interface_to_switch(id, switch_id) 27 | get(id) 28 | end 29 | 30 | def delete(id) 31 | service.delete_interface(id) 32 | true 33 | end 34 | end 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/network/router.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/model' 2 | 3 | module Fog 4 | module Network 5 | class SakuraCloud 6 | class Router < Fog::Model 7 | identity :id, :aliases => 'ID' 8 | attribute :name, :aliases => 'Name' 9 | attribute :description, :aliases => 'Description' 10 | attribute :networkmasklen, :aliases => 'NetworkMaskLen' 11 | attribute :bandwidthmbps, :aliases => 'BandWidthMbps' 12 | attribute :switch, :aliases => 'Switch' 13 | 14 | 15 | def delete 16 | service.delete_router(identity) 17 | true 18 | end 19 | alias_method :destroy, :delete 20 | 21 | def save 22 | requires :name, :networkmasklen 23 | Fog::Logger.warning("Create Router with public subnet") 24 | attached_switch = service.create_router(@attributes).body["Internet"] 25 | Fog::Logger.warning("Waiting available new router...") 26 | new_data = switch_available?(service, attached_switch["ID"]) 27 | id = new_data['internet']['ID'] 28 | merge_attributes(new_data['internet']) 29 | self.reload 30 | true 31 | end 32 | 33 | def collect_monitor(start_time = nil, end_time = nil) 34 | service.collect_monitor_router(identity, start_time, end_time).body["Data"] 35 | end 36 | 37 | def change_bandwidth(bandwidth) 38 | ## change_bandwidth returns router as new one. 39 | new_router = service.change_router_bandwidth(identity, bandwidth).body["Internet"] 40 | self.id = new_router['ID'] 41 | self.reload 42 | end 43 | 44 | def switch_available?(network, router_id) 45 | until network.switches.find {|r| r.internet != nil && r.internet["ID"] == router_id} 46 | print '.' 47 | sleep 2 48 | end 49 | ::JSON.parse((network.switches.find {|r| r.internet != nil && r.internet["ID"] == router_id}).to_json) 50 | end 51 | end 52 | end 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/network/routers.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/collection' 2 | require 'fog/sakuracloud/models/network/router' 3 | 4 | module Fog 5 | module Network 6 | class SakuraCloud 7 | class Routers < Fog::Collection 8 | model Fog::Network::SakuraCloud::Router 9 | 10 | def all 11 | load service.list_routers.body['Internet'] 12 | end 13 | 14 | def get(id) 15 | all.find { |f| f.id == id } 16 | rescue Fog::Errors::NotFound 17 | nil 18 | end 19 | 20 | def delete(id) 21 | service.delete_router(id) 22 | true 23 | end 24 | end 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/network/switch.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/model' 2 | 3 | module Fog 4 | module Network 5 | class SakuraCloud 6 | class Switch < Fog::Model 7 | identity :id, :aliases => 'ID' 8 | attribute :name, :aliases => 'Name' 9 | attribute :description, :aliases => 'Description' 10 | attribute :server_count, :aliases => 'ServerCount' 11 | attribute :appliance_count, :aliases => 'ApplianceCount' 12 | attribute :subnets, :aliases => 'Subnets' 13 | attribute :ipv6nets, :aliases => 'IPv6Nets' 14 | attribute :internet, :aliases => 'Internet' 15 | attribute :bridge, :aliases => 'Bridge' 16 | 17 | 18 | def delete 19 | service.delete_switch(identity) 20 | true 21 | end 22 | alias_method :destroy, :delete 23 | 24 | def save 25 | requires :name 26 | Fog::Logger.warning("Create Switch") 27 | data = service.create_switch(@attributes).body["Switch"] 28 | merge_attributes(data) 29 | true 30 | end 31 | 32 | end 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/network/switches.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/collection' 2 | require 'fog/sakuracloud/models/network/switch' 3 | 4 | module Fog 5 | module Network 6 | class SakuraCloud 7 | class Switches < Fog::Collection 8 | model Fog::Network::SakuraCloud::Switch 9 | 10 | def all 11 | load service.list_switches.body['Switches'] 12 | end 13 | 14 | def get(id) 15 | all.find { |f| f.id == id } 16 | rescue Fog::Errors::NotFound 17 | nil 18 | end 19 | 20 | def delete(id) 21 | service.delete_switch(id) 22 | true 23 | end 24 | end 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/script/note.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/model' 2 | 3 | module Fog 4 | module SakuraCloud 5 | class Script 6 | class Note < Fog::Model 7 | identity :id, :aliases => 'ID' 8 | attribute :name, :aliases => 'Name' 9 | attribute :note_class, :aliases => 'Class' 10 | attribute :scope, :aliases => 'Scope' 11 | attribute :content, :aliases => 'Content' 12 | attribute :description, :aliases => 'Description' 13 | attribute :remark, :aliases => 'Remark' 14 | attribute :availability, :aliases => 'Availability' 15 | attribute :icon, :aliases => 'Icon' 16 | 17 | 18 | def delete 19 | service.delete_note(identity) 20 | true 21 | end 22 | alias_method :destroy, :delete 23 | 24 | def save 25 | requires :name, :content 26 | if identity 27 | Fog::Logger.warning("Update Note #{identity}") 28 | data = service.modify_note(@attributes).body["Note"] 29 | else 30 | Fog::Logger.warning("Create New Note") 31 | data = service.create_note(@attributes).body["Note"] 32 | end 33 | merge_attributes(data) 34 | true 35 | end 36 | end 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/script/notes.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/collection' 2 | require 'fog/sakuracloud/models/script/note' 3 | 4 | module Fog 5 | module SakuraCloud 6 | class Script 7 | class Notes < Fog::Collection 8 | model Fog::SakuraCloud::Script::Note 9 | 10 | def all 11 | load service.list_notes.body['Notes'] 12 | end 13 | 14 | def get(id) 15 | all.find { |f| f.id == id } 16 | rescue Fog::Errors::NotFound 17 | nil 18 | end 19 | 20 | def delete(id) 21 | service.delete_note(id) 22 | true 23 | end 24 | end 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/volume/archive.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/model' 2 | 3 | module Fog 4 | module Volume 5 | class SakuraCloud 6 | class Archive < Fog::Model 7 | identity :id, :aliases => 'ID' 8 | attribute :name, :aliases => 'Name' 9 | attribute :size_mb, :aliases => 'SizeMB' 10 | attribute :plan, :aliases => 'Plan' 11 | end 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/volume/archives.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/collection' 2 | require 'fog/sakuracloud/models/volume/archive' 3 | 4 | module Fog 5 | module Volume 6 | class SakuraCloud 7 | class Archives < Fog::Collection 8 | model Fog::Volume::SakuraCloud::Archive 9 | 10 | def all 11 | load service.list_archives.body['Archives'] 12 | end 13 | 14 | def get(id) 15 | all.find { |f| f.id == id } 16 | rescue Fog::Errors::NotFound 17 | nil 18 | end 19 | end 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/volume/disk.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/model' 2 | 3 | module Fog 4 | module Volume 5 | class SakuraCloud 6 | class Disk < Fog::Model 7 | identity :id, :aliases => 'ID' 8 | attribute :name, :aliases => 'Name' 9 | attribute :connection, :aliases => 'Connection' 10 | attribute :availability, :aliases => 'Availability' 11 | attribute :plan, :aliases => 'Plan' 12 | attribute :size_mb, :aliases => 'SizeMB' 13 | attribute :source_disk, :aliases => 'SourceDisk' 14 | attribute :source_archive, :aliases => 'SourceArchive' 15 | 16 | def delete 17 | service.delete_disk(identity) 18 | true 19 | end 20 | alias_method :destroy, :delete 21 | 22 | def save 23 | requires :name, :plan 24 | options = { 25 | :source_archive => source_archive, 26 | :size_mb => size_mb 27 | } 28 | data = service.create_disk(name, plan, options).body["Disk"] 29 | merge_attributes(data) 30 | true 31 | end 32 | 33 | def configure(sshkey_id) 34 | requires :id 35 | service.configure_disk(id, sshkey_id ) 36 | true 37 | end 38 | 39 | def carve_hostname_on_disk(hostname) 40 | requires :id 41 | service.carve_hostname_on_disk(id, hostname ) 42 | true 43 | end 44 | 45 | def attach(server_id) 46 | service.attach_disk(id, server_id) 47 | true 48 | end 49 | 50 | def associate_ip(ipaddress, networkmasklen, defaultroute) 51 | subnet ={ 52 | :ipaddress => ipaddress, 53 | :networkmasklen => networkmasklen, 54 | :defaultroute => defaultroute 55 | } 56 | requires :id 57 | service.associate_ip_to_disk(id, subnet ) 58 | true 59 | end 60 | 61 | def register_script(notes) 62 | note_ids = [] 63 | note_ids << notes 64 | requires :id 65 | service.register_note_to_disk(id, note_ids.flatten ) 66 | true 67 | end 68 | end 69 | end 70 | end 71 | end 72 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/volume/disks.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/collection' 2 | require 'fog/sakuracloud/models/volume/disk' 3 | 4 | module Fog 5 | module Volume 6 | class SakuraCloud 7 | class Disks < Fog::Collection 8 | model Fog::Volume::SakuraCloud::Disk 9 | 10 | def all 11 | load service.list_disks.body['Disks'] 12 | end 13 | 14 | def get(id) 15 | all.find { |f| f.id == id } 16 | rescue Fog::Errors::NotFound 17 | nil 18 | end 19 | 20 | def delete(id) 21 | service.delete_disk(id) 22 | true 23 | end 24 | end 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/volume/plan.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/model' 2 | 3 | module Fog 4 | module Volume 5 | class SakuraCloud 6 | class Plan < Fog::Model 7 | identity :id, :aliases => 'ID' 8 | attribute :name, :aliases => 'Name' 9 | attribute :size, :aliases => 'Size' 10 | end 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/models/volume/plans.rb: -------------------------------------------------------------------------------- 1 | require 'fog/core/collection' 2 | require 'fog/sakuracloud/models/volume/plan' 3 | 4 | module Fog 5 | module Volume 6 | class SakuraCloud 7 | class Plans < Fog::Collection 8 | model Fog::Volume::SakuraCloud::Plan 9 | 10 | def all 11 | load service.list_plans.body['DiskPlans'] 12 | end 13 | 14 | def get(id) 15 | all.find { |f| f.id == id } 16 | rescue Fog::Errors::NotFound 17 | nil 18 | end 19 | end 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/network.rb: -------------------------------------------------------------------------------- 1 | module Fog 2 | module Network 3 | class SakuraCloud < Fog::Service 4 | requires :sakuracloud_api_token 5 | requires :sakuracloud_api_token_secret 6 | 7 | recognizes :sakuracloud_api_url, :api_zone 8 | 9 | model_path 'fog/sakuracloud/models/network' 10 | model :router 11 | collection :routers 12 | model :switch 13 | collection :switches 14 | model :interface 15 | collection :interfaces 16 | 17 | request_path 'fog/sakuracloud/requests/network' 18 | request :list_routers 19 | request :create_router 20 | request :delete_router 21 | request :collect_monitor_router 22 | request :change_router_bandwidth 23 | request :list_switches 24 | request :create_switch 25 | request :delete_switch 26 | request :list_interfaces 27 | request :regist_interface_to_server 28 | request :connect_interface_to_switch 29 | request :delete_interface 30 | 31 | class Real 32 | include Fog::SakuraCloud::Utils::Request 33 | 34 | def initialize(options = {}) 35 | @auth_encode = Base64.strict_encode64([ 36 | options[:sakuracloud_api_token], 37 | options[:sakuracloud_api_token_secret] 38 | ].join(':')) 39 | Fog.credentials[:sakuracloud_api_token] = options[:sakuracloud_api_token] 40 | Fog.credentials[:sakuracloud_api_token_secret] = options[:sakuracloud_api_token_secret] 41 | 42 | @sakuracloud_api_url = options[:sakuracloud_api_url] || 'https://secure.sakura.ad.jp' 43 | @api_zone = options[:api_zone] || Fog.credentials[:sakuracloud_api_zone] || 'is1b' 44 | Fog::SakuraCloud.validate_api_zone!(@api_zone) 45 | 46 | @connection = Fog::Core::Connection.new(@sakuracloud_api_url) 47 | end 48 | end 49 | 50 | class Mock 51 | def self.data 52 | @data ||= Hash.new do |hash, key| 53 | hash[key] = { 54 | :routers => [] 55 | } 56 | end 57 | end 58 | 59 | def self.reset 60 | @data = nil 61 | end 62 | 63 | def initialize(options={}) 64 | @sakuracloud_api_token = options[:sakuracloud_api_token] 65 | @sakuracloud_api_token_secret = options[:sakuracloud_api_token_secret] 66 | end 67 | 68 | def data 69 | self.class.data[@sakuracloud_api_token] 70 | self.class.data[@sakuracloud_api_token_secret] 71 | end 72 | 73 | def reset_data 74 | self.class.data.delete(@sakuracloud_api_token) 75 | self.class.data.delete(@sakuracloud_api_token_secret) 76 | end 77 | end 78 | end #SakuraCloud 79 | end #Network 80 | end 81 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/compute/boot_server.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Compute 5 | class SakuraCloud 6 | class Real 7 | def boot_server( id ) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :expects => [200], 13 | :method => 'PUT', 14 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/server/#{id}/power" 15 | ) 16 | true 17 | end 18 | end # Real 19 | 20 | class Mock 21 | def boot_server( id ) 22 | response = Excon::Response.new 23 | response.status = 200 24 | response.body = { 25 | } 26 | response 27 | end 28 | end 29 | end # SakuraCloud 30 | end # Volume 31 | end # Fog 32 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/compute/create_server.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Compute 5 | class SakuraCloud 6 | class Real 7 | def create_server(options) 8 | if options[:switch] 9 | switchs = options[:switch].split(',') 10 | connectedswitches = switchs.map do |sw_id| 11 | { 12 | "ID" => sw_id 13 | } 14 | end 15 | else 16 | connectedswitches = [{"Scope"=>"shared", "BandWidthMbps"=>100}] 17 | end 18 | 19 | body = { 20 | "Server" => { 21 | "Name" => options[:name], 22 | "ServerPlan" => { 23 | "ID" => options[:serverplan].to_i 24 | }, 25 | "ConnectedSwitches" => connectedswitches 26 | } 27 | } 28 | 29 | request( 30 | :headers => { 31 | 'Authorization' => "Basic #{@auth_encode}" 32 | }, 33 | :expects => [201], 34 | :method => 'POST', 35 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/server", 36 | :body => Fog::JSON.encode(body) 37 | ) 38 | end 39 | end # Real 40 | 41 | class Mock 42 | def create_server(options) 43 | response = Excon::Response.new 44 | response.status = 201 45 | response.body = { 46 | } 47 | response 48 | end 49 | end 50 | end # SakuraCloud 51 | end # Volume 52 | end # Fog 53 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/compute/delete_server.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Compute 5 | class SakuraCloud 6 | class Real 7 | def delete_server( id, force = false, disks = [] ) 8 | body = { "Force" => force, 'WithDisk' => disks } 9 | 10 | request( 11 | :headers => { 12 | 'Authorization' => "Basic #{@auth_encode}" 13 | }, 14 | :expects => [200], 15 | :method => 'DELETE', 16 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/server/#{id}", 17 | :body => Fog::JSON.encode(body) 18 | ) 19 | end 20 | end # Real 21 | 22 | class Mock 23 | def delete_server( id, force = false, disks = [] ) 24 | response = Excon::Response.new 25 | response.status = 200 26 | response.body = { 27 | } 28 | response 29 | end 30 | end 31 | end # SakuraCloud 32 | end # Volume 33 | end # Fog 34 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/compute/list_plans.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Compute 5 | class SakuraCloud 6 | class Real 7 | def list_plans(options = {}) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :method => 'GET', 13 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/product/server" 14 | ) 15 | end 16 | end 17 | 18 | class Mock 19 | def list_plans(options = {}) 20 | response = Excon::Response.new 21 | response.status = 200 22 | response.body = { 23 | "ServerPlans" => 24 | [ 25 | {"Index"=>0, 26 | "ID"=>1001, 27 | "Name"=>"プラン/1Core-1GB", 28 | "CPU"=>1, 29 | "MemoryMB"=>1024, 30 | "ServiceClass"=>"cloud/plan/1core-1gb", 31 | "Availability"=>"available"}, 32 | {"Index"=>1, 33 | "ID"=>2001, 34 | "Name"=>"プラン/1Core-2GB", 35 | "CPU"=>1, 36 | "MemoryMB"=>2048, 37 | "ServiceClass"=>"cloud/plan/1core-2gb", 38 | "Availability"=>"available"} 39 | ] 40 | } 41 | response 42 | end 43 | end 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/compute/list_servers.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Compute 5 | class SakuraCloud 6 | class Real 7 | def list_servers(options = {}) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :method => 'GET', 13 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/server" 14 | ) 15 | end 16 | end 17 | 18 | class Mock 19 | def list_servers(options = {}) 20 | response = Excon::Response.new 21 | response.status = 200 22 | response.body = { 23 | "Servers" => 24 | [ 25 | {"Index" => 0, 26 | "ID"=>112600055376, 27 | "Name"=>"foober1", 28 | "ServerPlan"=> {}, 29 | "Instance"=> {}, 30 | "Disks"=> []}, 31 | {"Index" => 1, 32 | "ID"=>112600055377, 33 | "Name"=>"foober2", 34 | "ServerPlan"=> {}, 35 | "Instance"=> {}, 36 | "Disks"=> []} 37 | ] 38 | } 39 | response 40 | end 41 | end 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/compute/list_ssh_keys.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Compute 5 | class SakuraCloud 6 | class Real 7 | def list_ssh_keys(options = {}) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :method => 'GET', 13 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/sshkey" 14 | ) 15 | end 16 | end 17 | 18 | class Mock 19 | def list_ssh_keys(options = {}) 20 | response = Excon::Response.new 21 | response.status = 200 22 | response.body = { 23 | "SSHKeys"=> 24 | [ 25 | {"Index"=>0, 26 | "ID"=>"888888888888", 27 | "Name"=>"foobar1", 28 | "PublicKey"=>"ssh-rsa dummy"}, 29 | {"Index"=>1, 30 | "ID"=>"999999999999", 31 | "Name"=>"foobar2", 32 | "PublicKey"=>"ssh-rsa dummy"} 33 | ] 34 | } 35 | response 36 | end 37 | end 38 | end 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/compute/list_zones.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Compute 5 | class SakuraCloud 6 | class Real 7 | def list_zones(options = {}) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :method => 'GET', 13 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/zone" 14 | ) 15 | end 16 | end 17 | 18 | class Mock 19 | def list_zones(options = {}) 20 | response = Excon::Response.new 21 | response.status = 200 22 | response.body = { 23 | "Zones" => 24 | [ 25 | {"Index"=>0, 26 | "ID"=>31001, 27 | "Name"=>"is1a", 28 | "Description"=>"石狩第1ゾーン"}, 29 | {"Index"=>1, 30 | "ID"=>31002, 31 | "Name"=>"is1b", 32 | "Description"=>"石狩第2ゾーン"} 33 | ] 34 | } 35 | response 36 | end 37 | end 38 | end 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/compute/stop_server.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Compute 5 | class SakuraCloud 6 | class Real 7 | def stop_server( id, force = false ) 8 | if force 9 | body = { "Force" => true } 10 | else 11 | body = {} 12 | end 13 | request( 14 | :headers => { 15 | 'Authorization' => "Basic #{@auth_encode}" 16 | }, 17 | :expects => [200,202], 18 | :method => 'DELETE', 19 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/server/#{id}/power", 20 | :body => Fog::JSON.encode(body) 21 | ) 22 | true 23 | end 24 | end # Real 25 | 26 | class Mock 27 | def stop_server( id, force = false ) 28 | response = Excon::Response.new 29 | response.status = 202 30 | response.body = { 31 | } 32 | response 33 | end 34 | end 35 | end # SakuraCloud 36 | end # Volume 37 | end # Fog 38 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/dns/create_zone.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module DNS 5 | class SakuraCloud 6 | class Real 7 | def create_zone(options) 8 | name = options[:name] ? options[:name] : options[:zone] 9 | body = { 10 | "CommonServiceItem"=>{ 11 | "Name"=>name, 12 | "Status"=>{"Zone"=>options[:zone]}, 13 | "Provider"=>{"Class"=>"dns"}, 14 | "Description"=> options[:description], 15 | "Settings" => { 16 | "DNS" => { 17 | "ResourceRecordSets" => options[:rr_sets] 18 | } 19 | } 20 | } 21 | } 22 | 23 | request( 24 | :headers => { 25 | 'Authorization' => "Basic #{@auth_encode}" 26 | }, 27 | :expects => 201, 28 | :method => 'POST', 29 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/commonserviceitem", 30 | :body => Fog::JSON.encode(body) 31 | ) 32 | end 33 | end # Real 34 | 35 | class Mock 36 | def create_zone(options) 37 | response = Excon::Response.new 38 | response.status = 201 39 | response.body = { 40 | } 41 | response 42 | end 43 | end 44 | end # SakuraCloud 45 | end # DNS 46 | end # Fog 47 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/dns/delete_zone.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module DNS 5 | class SakuraCloud 6 | class Real 7 | def delete_zone( id ) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :expects => [200], 13 | :method => 'DELETE', 14 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/commonserviceitem/#{id}" 15 | ) 16 | end 17 | end # Real 18 | 19 | class Mock 20 | def delete_zone( id ) 21 | response = Excon::Response.new 22 | response.status = 200 23 | response.body = { 24 | } 25 | response 26 | end 27 | end 28 | end # SakuraCloud 29 | end # DNS 30 | end # Fog 31 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/dns/list_zones.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module DNS 5 | class SakuraCloud 6 | class Real 7 | def list_zones(options = {}) 8 | filter = { 9 | "Filter" => { 10 | "Provider.Class" => "dns" 11 | } 12 | } 13 | request( 14 | :headers => { 15 | 'Authorization' => "Basic #{@auth_encode}" 16 | }, 17 | :method => 'GET', 18 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/commonserviceitem", 19 | :query => URI.encode(Fog::JSON.encode(filter)) 20 | ) 21 | end 22 | end 23 | 24 | class Mock 25 | def list_zones(options = {}) 26 | response = Excon::Response.new 27 | response.status = 200 28 | response.body = { 29 | } 30 | response 31 | end 32 | end 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/dns/modify_zone.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module DNS 5 | class SakuraCloud 6 | class Real 7 | def modify_zone(options) 8 | body = { 9 | "CommonServiceItem"=>{ 10 | "Settings" => options[:settings] 11 | } 12 | } 13 | 14 | request( 15 | :headers => { 16 | 'Authorization' => "Basic #{@auth_encode}" 17 | }, 18 | :expects => 200, 19 | :method => 'PUT', 20 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/commonserviceitem/#{options[:id]}", 21 | :body => Fog::JSON.encode(body) 22 | ) 23 | end 24 | end # Real 25 | 26 | class Mock 27 | def modify_zone(options) 28 | response = Excon::Response.new 29 | response.status = 200 30 | response.body = { 31 | } 32 | response 33 | end 34 | end 35 | end # SakuraCloud 36 | end # DNS 37 | end # Fog 38 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/network/change_router_bandwidth.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Network 5 | class SakuraCloud 6 | class Real 7 | def change_router_bandwidth( id, bandwidthmbps ) 8 | body = { 9 | "Internet" => { 10 | "BandWidthMbps" => bandwidthmbps 11 | } 12 | } 13 | 14 | request( 15 | :headers => { 16 | 'Authorization' => "Basic #{@auth_encode}" 17 | }, 18 | :expects => [200], 19 | :method => 'PUT', 20 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/internet/#{id}/bandwidth", 21 | :body => Fog::JSON.encode(body) 22 | ) 23 | end 24 | end # Real 25 | 26 | class Mock 27 | def change_router_bandwidth( id, bandwidthmbps ) 28 | response = Excon::Response.new 29 | response.status = 200 30 | response.body = { 31 | "Internet"=>{"ID"=>"112701091977"}, 32 | "Success"=>true, 33 | "is_ok"=>true 34 | } 35 | response 36 | end 37 | end # Mock 38 | end # SakuraCloud 39 | end # Network 40 | end # Fog 41 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/network/collect_monitor_router.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Network 5 | class SakuraCloud 6 | class Real 7 | def collect_monitor_router( id ,start_time = nil, end_time = nil) 8 | filter = {} 9 | filter['Start'] = start_time if start_time 10 | filter['End'] = end_time if end_time 11 | request( 12 | :headers => { 13 | 'Authorization' => "Basic #{@auth_encode}" 14 | }, 15 | :expects => [200], 16 | :method => 'GET', 17 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/internet/#{id}/monitor", 18 | :query => URI.encode(Fog::JSON.encode(filter)) 19 | ) 20 | end 21 | end # Real 22 | 23 | class Mock 24 | def collect_monitor_router( id ) 25 | response = Excon::Response.new 26 | response.status = 200 27 | response.body = { 28 | "Data"=>{ 29 | "2015-12-16T18:00:00+09:00"=>{ 30 | "In"=>500000, 31 | "Out"=>70000000 32 | } 33 | }, 34 | "is_ok"=>true 35 | } 36 | response 37 | end 38 | end # Mock 39 | end # SakuraCloud 40 | end # Network 41 | end # Fog 42 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/network/connect_interface_to_switch.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Network 5 | class SakuraCloud 6 | class Real 7 | def connect_interface_to_switch( id, switch_id ) 8 | response = request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :expects => [200], 13 | :method => 'PUT', 14 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/interface/#{id}/to/switch/#{switch_id}" 15 | ) 16 | response.body['Interface']['ID'] 17 | end 18 | end # Real 19 | 20 | class Mock 21 | def regist_interface_to_server( id, switch_id ) 22 | response = Excon::Response.new 23 | response.status = 20 24 | response.body = { 25 | } 26 | response 27 | end 28 | end 29 | end # SakuraCloud 30 | end # Network0 31 | end # Fog 32 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/network/create_router.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Network 5 | class SakuraCloud 6 | class Real 7 | def create_router(options) 8 | bandwidthmbps = options[:bandwidthmbps] ? options[:bandwidthmbps].to_i : 100 9 | 10 | body = { 11 | "Internet" => { 12 | "Name" => options[:name], 13 | "NetworkMaskLen"=> options[:networkmasklen].to_i, 14 | "BandWidthMbps"=> bandwidthmbps 15 | } 16 | } 17 | 18 | request( 19 | :headers => { 20 | 'Authorization' => "Basic #{@auth_encode}" 21 | }, 22 | :expects => 202, 23 | :method => 'POST', 24 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/internet", 25 | :body => Fog::JSON.encode(body) 26 | ) 27 | end 28 | end # Real 29 | 30 | class Mock 31 | def create_router(options) 32 | response = Excon::Response.new 33 | response.status = 202 34 | response.body = { 35 | } 36 | response 37 | end 38 | end 39 | end # SakuraCloud 40 | end # Network 41 | end # Fog 42 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/network/create_switch.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Network 5 | class SakuraCloud 6 | class Real 7 | def create_switch(options) 8 | body = { 9 | "Switch" => { 10 | "Name" => options[:name] 11 | } 12 | } 13 | 14 | request( 15 | :headers => { 16 | 'Authorization' => "Basic #{@auth_encode}" 17 | }, 18 | :expects => 201, 19 | :method => 'POST', 20 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/switch", 21 | :body => Fog::JSON.encode(body) 22 | ) 23 | end 24 | end # Real 25 | 26 | class Mock 27 | def create_switch(options) 28 | response = Excon::Response.new 29 | response.status = 201 30 | response.body = { 31 | } 32 | response 33 | end 34 | end 35 | end # SakuraCloud 36 | end # Network 37 | end # Fog 38 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/network/delete_interface.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Network 5 | class SakuraCloud 6 | class Real 7 | def delete_interface( id ) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :expects => [200], 13 | :method => 'DELETE', 14 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/interface/#{id}" 15 | ) 16 | end 17 | end # Real 18 | 19 | class Mock 20 | def delete_interface( id ) 21 | response = Excon::Response.new 22 | response.status = 200 23 | response.body = { 24 | } 25 | response 26 | end 27 | end 28 | end # SakuraCloud 29 | end # Network 30 | end # Fog 31 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/network/delete_router.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Network 5 | class SakuraCloud 6 | class Real 7 | def delete_router( id ) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :expects => [200], 13 | :method => 'DELETE', 14 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/internet/#{id}" 15 | ) 16 | end 17 | end # Real 18 | 19 | class Mock 20 | def delete_router( id ) 21 | response = Excon::Response.new 22 | response.status = 200 23 | response.body = { 24 | } 25 | response 26 | end 27 | end 28 | end # SakuraCloud 29 | end # Network 30 | end # Fog 31 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/network/delete_switch.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Network 5 | class SakuraCloud 6 | class Real 7 | def delete_switch( id ) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :expects => [200], 13 | :method => 'DELETE', 14 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/switch/#{id}" 15 | ) 16 | end 17 | end # Real 18 | 19 | class Mock 20 | def delete_switch( id ) 21 | response = Excon::Response.new 22 | response.status = 200 23 | response.body = { 24 | } 25 | response 26 | end 27 | end 28 | end # SakuraCloud 29 | end # Network 30 | end # Fog 31 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/network/list_interfaces.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Network 5 | class SakuraCloud 6 | class Real 7 | def list_interfaces(options = {}) 8 | filter = { 9 | "Include" => [ 10 | "ID", 11 | "MACAddress", 12 | "IPAddress", 13 | "UserIPAddress", 14 | "Switch.ID", 15 | "Server.ID"] 16 | } 17 | request( 18 | :headers => { 19 | 'Authorization' => "Basic #{@auth_encode}" 20 | }, 21 | :method => 'GET', 22 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/interface", 23 | :query => URI.encode(Fog::JSON.encode(filter)) 24 | ) 25 | end 26 | end 27 | 28 | class Mock 29 | def list_interfaces(options = {}) 30 | response = Excon::Response.new 31 | response.status = 200 32 | response.body = { 33 | } 34 | response 35 | end 36 | end 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/network/list_routers.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Network 5 | class SakuraCloud 6 | class Real 7 | def list_routers(options = {}) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :method => 'GET', 13 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/internet" 14 | ) 15 | end 16 | end 17 | 18 | class Mock 19 | def list_routers(options = {}) 20 | response = Excon::Response.new 21 | response.status = 200 22 | response.body = { 23 | "Internet"=>[ 24 | {"Index"=>0, 25 | "ID"=>"112600707538", 26 | "Switch"=>{ 27 | "ID"=>"112600707539", 28 | "Name"=>"router2" 29 | } 30 | } 31 | ], 32 | "is_ok"=>true 33 | } 34 | response 35 | end 36 | end 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/network/list_switches.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Network 5 | class SakuraCloud 6 | class Real 7 | def list_switches(options = {}) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :method => 'GET', 13 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/switch" 14 | ) 15 | end 16 | end 17 | 18 | class Mock 19 | def list_switches(options = {}) 20 | response = Excon::Response.new 21 | response.status = 200 22 | response.body = { 23 | "Switches"=> 24 | [{"Index"=>0, 25 | "ID"=>"112600703732", 26 | "Name"=>"foobar1", 27 | "Description"=>"", 28 | "ServerCount"=>0, 29 | "ApplianceCount"=>0, 30 | "HybridConnection"=>nil, 31 | "ServiceClass"=>"cloud/switch/default", 32 | "CreatedAt"=>"2014-09-05T16:35:41+09:00", 33 | "Subnets"=> 34 | [{"ID"=>nil, 35 | "NetworkAddress"=>nil, 36 | "NetworkMaskLen"=>nil, 37 | "DefaultRoute"=>nil, 38 | "NextHop"=>nil, 39 | "StaticRoute"=>nil, 40 | "ServiceClass"=>nil, 41 | "IPAddresses"=>{"Min"=>nil, "Max"=>nil}, 42 | "Internet"=>{"ID"=>nil, "Name"=>nil, "BandWidthMbps"=>nil, "ServiceClass"=>nil}}], 43 | "IPv6Nets"=>[], 44 | "Internet"=>nil, 45 | "Bridge"=>nil}, 46 | {"Index"=>1, 47 | "ID"=>"112600703734", 48 | "Name"=>"foobar2", 49 | "Description"=>"", 50 | "ServerCount"=>1, 51 | "ApplianceCount"=>0, 52 | "HybridConnection"=>nil, 53 | "ServiceClass"=>"cloud/switch/default", 54 | "CreatedAt"=>"2014-09-05T16:36:13+09:00", 55 | "Subnets"=> 56 | [{"ID"=>1036, 57 | "NetworkAddress"=>"133.242.241.240", 58 | "NetworkMaskLen"=>28, 59 | "DefaultRoute"=>"133.242.241.241", 60 | "NextHop"=>nil, 61 | "StaticRoute"=>nil, 62 | "ServiceClass"=>"cloud/global-ipaddress-v4/28", 63 | "IPAddresses"=>{"Min"=>"133.242.241.244", "Max"=>"133.242.241.254"}, 64 | "Internet"=>{"ID"=>"112600703733", "Name"=>"hogehoge2", "BandWidthMbps"=>100, "ServiceClass"=>"cloud/internet/router/100m"}}], 65 | "IPv6Nets"=>[], 66 | "Internet"=>{"ID"=>"112600703733", "Name"=>"hogehoge2", "BandWidthMbps"=>100, "Scope"=>"user", "ServiceClass"=>"cloud/internet/router/100m"}, 67 | "Bridge"=>nil}] 68 | } 69 | response 70 | end 71 | end 72 | end 73 | end 74 | end 75 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/network/regist_interface_to_server.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Network 5 | class SakuraCloud 6 | class Real 7 | def regist_interface_to_server( server_id ) 8 | body = { 9 | "Interface" => { 10 | "Server" => { 11 | "ID" => server_id 12 | } 13 | } 14 | } 15 | 16 | response = request( 17 | :headers => { 18 | 'Authorization' => "Basic #{@auth_encode}" 19 | }, 20 | :expects => [201], 21 | :method => 'POST', 22 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/interface", 23 | :body => Fog::JSON.encode(body) 24 | ) 25 | response.body['Interface']['ID'] 26 | end 27 | end # Real 28 | 29 | class Mock 30 | def regist_interface_to_server( id ) 31 | response = Excon::Response.new 32 | response.status = 201 33 | response.body = { 34 | } 35 | response 36 | end 37 | end 38 | end # SakuraCloud 39 | end # Network 40 | end # Fog 41 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/script/create_note.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module SakuraCloud 5 | class Script 6 | class Real 7 | def create_note(options) 8 | body = { 9 | "Note" => { 10 | "Name" => options[:name], 11 | "Content" => options[:content] 12 | } 13 | } 14 | 15 | request( 16 | :headers => { 17 | 'Authorization' => "Basic #{@auth_encode}" 18 | }, 19 | :expects => 201, 20 | :method => 'POST', 21 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/note", 22 | :body => Fog::JSON.encode(body) 23 | ) 24 | end 25 | end # Real 26 | 27 | class Mock 28 | def create_note(options) 29 | response = Excon::Response.new 30 | response.status = 201 31 | response.body = { 32 | "Note"=> 33 | {"ID"=>"112700759852", 34 | "Name"=>"foobar", 35 | "Class"=>"shell", 36 | "Scope"=>"user", 37 | "Content"=>"#!/bin/bash", 38 | "Description"=>"", 39 | "Remark"=>nil, 40 | "Availability"=>"available", 41 | "CreatedAt"=>"2015-09-05T20:32:12+09:00", 42 | "ModifiedAt"=>"2015-09-05T20:32:12+09:00", 43 | "Icon"=>nil, 44 | "Tags"=>[]}, 45 | "Success"=>true, 46 | "is_ok"=>true} 47 | response 48 | end 49 | end 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/script/delete_note.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module SakuraCloud 5 | class Script 6 | class Real 7 | def delete_note( id ) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :expects => [200], 13 | :method => 'DELETE', 14 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/note/#{id}" 15 | ) 16 | end 17 | end # Real 18 | 19 | class Mock 20 | def delete_note( id ) 21 | response = Excon::Response.new 22 | response.status = 200 23 | response.body = { 24 | "Note"=> 25 | {"ID"=>"112700759822", 26 | "Name"=>"hogehoge2", 27 | "Class"=>"shell", 28 | "Scope"=>"user", 29 | "Content"=>"", 30 | "Description"=>"", 31 | "Remark"=>nil, 32 | "Availability"=>"available", 33 | "CreatedAt"=>"2015-09-05T20:04:24+09:00", 34 | "ModifiedAt"=>"2015-09-05T20:04:24+09:00", 35 | "Icon"=>nil, 36 | "Tags"=>[]}, 37 | "Success"=>true, 38 | "is_ok"=>true 39 | } 40 | response 41 | end 42 | end 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/script/list_notes.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module SakuraCloud 5 | class Script 6 | class Real 7 | def list_notes(options = {}) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :method => 'GET', 13 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/note" 14 | ) 15 | end 16 | end 17 | 18 | class Mock 19 | def list_notes(options = {}) 20 | response = Excon::Response.new 21 | response.status = 200 22 | response.body = { 23 | "Internet"=>[ 24 | {"Index"=>0, 25 | "ID"=>"112600707538", 26 | "Switch"=>{ 27 | "ID"=>"112600707539", 28 | "Name"=>"router2" 29 | } 30 | } 31 | ], 32 | "is_ok"=>true 33 | } 34 | response 35 | end 36 | end 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/script/modify_note.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module SakuraCloud 5 | class Script 6 | class Real 7 | def modify_note( options ) 8 | body = { 9 | "Note" => { 10 | "Name" => options[:name], 11 | "Content" => options[:content] 12 | } 13 | } 14 | 15 | request( 16 | :headers => { 17 | 'Authorization' => "Basic #{@auth_encode}" 18 | }, 19 | :expects => [200], 20 | :method => 'PUT', 21 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/note/#{options[:id]}", 22 | :body => Fog::JSON.encode(body) 23 | ) 24 | end 25 | end # Real 26 | 27 | class Mock 28 | def modify_note( options ) 29 | response = Excon::Response.new 30 | response.status = 200 31 | response.body = { 32 | "Note"=> 33 | {"ID"=>"112700759822", 34 | "Name"=>"hogehoge2", 35 | "Class"=>"shell", 36 | "Scope"=>"user", 37 | "Content"=>"", 38 | "Description"=>"", 39 | "Remark"=>nil, 40 | "Availability"=>"available", 41 | "CreatedAt"=>"2015-09-05T20:04:24+09:00", 42 | "ModifiedAt"=>"2015-09-05T20:04:24+09:00", 43 | "Icon"=>nil, 44 | "Tags"=>[]}, 45 | "Success"=>true, 46 | "is_ok"=>true 47 | } 48 | response 49 | end 50 | end 51 | end 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/volume/associate_ip_to_disk.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Volume 5 | class SakuraCloud 6 | class Real 7 | def associate_ip_to_disk( disk_id, subnet ) 8 | body = { 9 | "UserIPAddress" => subnet[:ipaddress], 10 | "UserSubnet" => { 11 | "NetworkMaskLen" => subnet[:networkmasklen], 12 | "DefaultRoute" => subnet[:defaultroute] 13 | } 14 | } 15 | 16 | request( 17 | :headers => { 18 | 'Authorization' => "Basic #{@auth_encode}" 19 | }, 20 | :expects => [200], 21 | :method => 'PUT', 22 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/disk/#{disk_id.to_s}/config", 23 | :body => Fog::JSON.encode(body) 24 | ) 25 | end 26 | end # Real 27 | 28 | class Mock 29 | def associate_ip_to_disk( disk_id, subnet ) 30 | response = Excon::Response.new 31 | response.status = 200 32 | response.body = { 33 | } 34 | response 35 | end 36 | end 37 | end # SakuraCloud 38 | end # Volume 39 | end # Fog 40 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/volume/attach_disk.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Volume 5 | class SakuraCloud 6 | class Real 7 | def attach_disk( disk_id, server_id ) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :expects => [200], 13 | :method => 'PUT', 14 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/disk/#{disk_id.to_s}/to/server/#{server_id.to_s}" 15 | ) 16 | end 17 | end # Real 18 | 19 | class Mock 20 | def attach_disk( disk_id, server_id ) 21 | response = Excon::Response.new 22 | response.status = 200 23 | response.body = { 24 | } 25 | response 26 | end 27 | end 28 | end # SakuraCloud 29 | end # Volume 30 | end # Fog 31 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/volume/carve_hostname_on_disk.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Volume 5 | class SakuraCloud 6 | class Real 7 | def carve_hostname_on_disk( disk_id, hostname ) 8 | body = { 9 | "HostName" => hostname 10 | } 11 | 12 | request( 13 | :headers => { 14 | 'Authorization' => "Basic #{@auth_encode}" 15 | }, 16 | :expects => [200], 17 | :method => 'PUT', 18 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/disk/#{disk_id.to_s}/config", 19 | :body => Fog::JSON.encode(body) 20 | ) 21 | end 22 | end # Real 23 | 24 | class Mock 25 | def carve_hostname_on_disk( disk_id, hostname ) 26 | response = Excon::Response.new 27 | response.status = 200 28 | response.body = {"Success"=>true, "is_ok"=>true} 29 | response 30 | end 31 | end 32 | end # SakuraCloud 33 | end # Volume 34 | end # Fog 35 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/volume/configure_disk.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Volume 5 | class SakuraCloud 6 | class Real 7 | def configure_disk( disk_id, sshkey_id ) 8 | body = { 9 | "SSHKey" => {"ID" => sshkey_id.to_s } 10 | } 11 | 12 | request( 13 | :headers => { 14 | 'Authorization' => "Basic #{@auth_encode}" 15 | }, 16 | :expects => [200], 17 | :method => 'PUT', 18 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/disk/#{disk_id.to_s}/config", 19 | :body => Fog::JSON.encode(body) 20 | ) 21 | end 22 | end # Real 23 | 24 | class Mock 25 | def configure_disk( disk_id, sshkey_id ) 26 | response = Excon::Response.new 27 | response.status = 200 28 | response.body = {"Success"=>true, "is_ok"=>true} 29 | response 30 | end 31 | end 32 | end # SakuraCloud 33 | end # Volume 34 | end # Fog 35 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/volume/create_disk.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Volume 5 | class SakuraCloud 6 | class Real 7 | def create_disk( name, plan, options = {} ) 8 | body = { "Disk" => { "Name" => name, "Plan" => { "ID" => plan.to_i } } } 9 | 10 | if !options[:source_archive].nil? 11 | body["Disk"]["SourceArchive"] = { "ID"=>options[:source_archive].to_s } 12 | end 13 | 14 | if !options[:size_mb].nil? 15 | body["Disk"]["SizeMB"] = options[:size_mb].to_i 16 | end 17 | 18 | request( 19 | :headers => { 20 | 'Authorization' => "Basic #{@auth_encode}" 21 | }, 22 | :expects => [201, 202], 23 | :method => 'POST', 24 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/disk", 25 | :body => Fog::JSON.encode(body) 26 | ) 27 | end 28 | end # Real 29 | 30 | class Mock 31 | def create_disk( name, plan, options = {} ) 32 | response = Excon::Response.new 33 | response.status = 202 34 | response.body = { 35 | } 36 | response 37 | end 38 | end 39 | end # SakuraCloud 40 | end # Volume 41 | end # Fog 42 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/volume/delete_disk.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Volume 5 | class SakuraCloud 6 | class Real 7 | def delete_disk( id ) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :expects => [200], 13 | :method => 'DELETE', 14 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/disk/#{id}" 15 | ) 16 | end 17 | end # Real 18 | 19 | class Mock 20 | def delete_disk( id ) 21 | response = Excon::Response.new 22 | response.status = 200 23 | response.body = { 24 | } 25 | response 26 | end 27 | end 28 | end # SakuraCloud 29 | end # Volume 30 | end # Fog 31 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/volume/list_archives.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Volume 5 | class SakuraCloud 6 | class Real 7 | def list_archives(options = {}) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :method => 'GET', 13 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/archive" 14 | ) 15 | end 16 | end 17 | 18 | class Mock 19 | def list_archives(options = {}) 20 | response = Excon::Response.new 21 | response.status = 200 22 | response.body = { 23 | "Archives" => 24 | [ 25 | {"Index"=>0, 26 | "ID"=>112500514887, 27 | "Name"=>"CentOS 5.10 64bit (基本セット)", 28 | "Availability"=>"available", 29 | "SizeMB"=>20480, 30 | "Plan"=>{"ID"=>2, "StorageClass"=>"iscsi1204", "Name"=>"標準プラン"} 31 | }, 32 | {"Index"=>1, 33 | "ID"=>112500571575, 34 | "Name"=>"CentOS 6.5 64bit (基本セット)", 35 | "Availability"=>"available", 36 | "SizeMB"=>102400, 37 | "Plan"=>{"ID"=>2, "StorageClass"=>"iscsi1204", "Name"=>"標準プラン"} 38 | } 39 | ] 40 | } 41 | response 42 | end 43 | end 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/volume/list_disks.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Volume 5 | class SakuraCloud 6 | class Real 7 | def list_disks(options = {}) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :method => 'GET', 13 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/disk" 14 | ) 15 | end 16 | end 17 | 18 | class Mock 19 | def list_disks(options = {}) 20 | response = Excon::Response.new 21 | response.status = 200 22 | response.body = { 23 | "Disks" => 24 | [ 25 | {"Index" => 0, 26 | "ID" =>112600053890, 27 | "Name" =>"foober1", 28 | "Connection" => "virtio", 29 | "Availability"=>"available", 30 | "SizeMB"=>20480, 31 | "Plan"=> {}, 32 | "SourceDisk" => nil, 33 | "SourceArchive" => {}}, 34 | {"Index" => 1, 35 | "ID" =>112600053891, 36 | "Name" =>"foober2", 37 | "Connection" => "virtio", 38 | "Availability"=>"available", 39 | "SizeMB"=>20480, 40 | "Plan"=> {}, 41 | "SourceDisk" => nil, 42 | "SourceArchive" => {}} 43 | ] 44 | } 45 | response 46 | end 47 | end 48 | end 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/volume/list_plans.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Volume 5 | class SakuraCloud 6 | class Real 7 | def list_plans(options = {}) 8 | request( 9 | :headers => { 10 | 'Authorization' => "Basic #{@auth_encode}" 11 | }, 12 | :method => 'GET', 13 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/product/disk" 14 | ) 15 | end 16 | end 17 | 18 | class Mock 19 | def list_plans(options = {}) 20 | response = Excon::Response.new 21 | response.status = 200 22 | response.body = { 23 | "DiskPlans" => 24 | [ 25 | {"Index"=>0, 26 | "ID"=>4, 27 | "Name"=>"SSDプラン", 28 | "Availability"=>"available", 29 | "Size"=> 30 | [{"SizeMB"=>20480, "DisplaySize"=>20, "DisplaySuffix"=>"GB", "Availability"=>"available", "ServiceClass"=>"cloud/disk/ssd/20g"}, 31 | {"SizeMB"=>102400, "DisplaySize"=>100, "DisplaySuffix"=>"GB", "Availability"=>"available", "ServiceClass"=>"cloud/disk/ssd/100g"}, 32 | {"SizeMB"=>256000, "DisplaySize"=>250, "DisplaySuffix"=>"GB", "Availability"=>"available", "ServiceClass"=>"cloud/disk/ssd/250g"}, 33 | {"SizeMB"=>512000, "DisplaySize"=>500, "DisplaySuffix"=>"GB", "Availability"=>"available", "ServiceClass"=>"cloud/disk/ssd/500g"}]}, 34 | {"Index"=>1, 35 | "ID"=>2, 36 | "Name"=>"標準プラン", 37 | "Availability"=>"available", 38 | "Size"=> 39 | [{"SizeMB"=>40960, "DisplaySize"=>40, "DisplaySuffix"=>"GB", "Availability"=>"available", "ServiceClass"=>"cloud/disk/iscsi/40g"}, 40 | {"SizeMB"=>61440, "DisplaySize"=>60, "DisplaySuffix"=>"GB", "Availability"=>"available", "ServiceClass"=>"cloud/disk/iscsi/60g"}, 41 | {"SizeMB"=>81920, "DisplaySize"=>80, "DisplaySuffix"=>"GB", "Availability"=>"available", "ServiceClass"=>"cloud/disk/iscsi/80g"}, 42 | {"SizeMB"=>102400, "DisplaySize"=>100, "DisplaySuffix"=>"GB", "Availability"=>"available", "ServiceClass"=>"cloud/disk/iscsi/100g"}, 43 | {"SizeMB"=>256000, "DisplaySize"=>250, "DisplaySuffix"=>"GB", "Availability"=>"available", "ServiceClass"=>"cloud/disk/iscsi/250g"}, 44 | {"SizeMB"=>512000, "DisplaySize"=>500, "DisplaySuffix"=>"GB", "Availability"=>"available", "ServiceClass"=>"cloud/disk/iscsi/500g"}, 45 | {"SizeMB"=>768000, "DisplaySize"=>750, "DisplaySuffix"=>"GB", "Availability"=>"available", "ServiceClass"=>"cloud/disk/iscsi/750g"}, 46 | {"SizeMB"=>1048576, "DisplaySize"=>1, "DisplaySuffix"=>"TB", "Availability"=>"available", "ServiceClass"=>"cloud/disk/iscsi/1t"}, 47 | {"SizeMB"=>2097152, "DisplaySize"=>2, "DisplaySuffix"=>"TB", "Availability"=>"available", "ServiceClass"=>"cloud/disk/iscsi/2t"}, 48 | {"SizeMB"=>4194304, "DisplaySize"=>4, "DisplaySuffix"=>"TB", "Availability"=>"available", "ServiceClass"=>"cloud/disk/iscsi/4t"}]} 49 | ] 50 | } 51 | response 52 | end 53 | end 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/requests/volume/register_note_to_disk.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | module Fog 4 | module Volume 5 | class SakuraCloud 6 | class Real 7 | def register_note_to_disk( disk_id, notes ) 8 | body = { 9 | "Notes" => notes.map {|note| { "ID" => note }} 10 | } 11 | 12 | request( 13 | :headers => { 14 | 'Authorization' => "Basic #{@auth_encode}" 15 | }, 16 | :expects => [200], 17 | :method => 'PUT', 18 | :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/disk/#{disk_id.to_s}/config", 19 | :body => Fog::JSON.encode(body) 20 | ) 21 | end 22 | end # Real 23 | 24 | class Mock 25 | def register_note_to_disk( disk_id, notes ) 26 | response = Excon::Response.new 27 | response.status = 200 28 | response.body = { 29 | } 30 | response 31 | end 32 | end 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/script.rb: -------------------------------------------------------------------------------- 1 | module Fog 2 | module SakuraCloud 3 | class Script < Fog::Service 4 | requires :sakuracloud_api_token 5 | requires :sakuracloud_api_token_secret 6 | 7 | recognizes :sakuracloud_api_url, :api_zone 8 | 9 | model_path 'fog/sakuracloud/models/script' 10 | model :note 11 | collection :notes 12 | 13 | request_path 'fog/sakuracloud/requests/script' 14 | request :list_notes 15 | request :create_note 16 | request :modify_note 17 | request :delete_note 18 | 19 | class Real 20 | include Fog::SakuraCloud::Utils::Request 21 | 22 | def initialize(options = {}) 23 | @auth_encode = Base64.strict_encode64([ 24 | options[:sakuracloud_api_token], 25 | options[:sakuracloud_api_token_secret] 26 | ].join(':')) 27 | Fog.credentials[:sakuracloud_api_token] = options[:sakuracloud_api_token] 28 | Fog.credentials[:sakuracloud_api_token_secret] = options[:sakuracloud_api_token_secret] 29 | 30 | @sakuracloud_api_url = options[:sakuracloud_api_url] || 'https://secure.sakura.ad.jp' 31 | @api_zone = options[:api_zone] || Fog.credentials[:sakuracloud_api_zone] || 'is1b' 32 | Fog::SakuraCloud.validate_api_zone!(@api_zone) 33 | 34 | @connection = Fog::Core::Connection.new(@sakuracloud_api_url) 35 | end 36 | end 37 | 38 | class Mock 39 | def self.data 40 | @data ||= Hash.new do |hash, key| 41 | hash[key] = { 42 | :notes => [] 43 | } 44 | end 45 | end 46 | 47 | def self.reset 48 | @data = nil 49 | end 50 | 51 | def initialize(options={}) 52 | @sakuracloud_api_token = options[:sakuracloud_api_token] 53 | @sakuracloud_api_token_secret = options[:sakuracloud_api_token_secret] 54 | end 55 | 56 | def data 57 | self.class.data[@sakuracloud_api_token] 58 | self.class.data[@sakuracloud_api_token_secret] 59 | end 60 | 61 | def reset_data 62 | self.class.data.delete(@sakuracloud_api_token) 63 | self.class.data.delete(@sakuracloud_api_token_secret) 64 | end 65 | end 66 | end 67 | end 68 | end 69 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/utils/request.rb: -------------------------------------------------------------------------------- 1 | module Fog 2 | module SakuraCloud 3 | module Utils 4 | module Request 5 | def request(params) 6 | response = parse @connection.request(params) 7 | 8 | response 9 | ## SakuraCloud API returns Japanese message. 10 | # This wrapper decodes and show message to be human readble. 11 | rescue Excon::Errors::HTTPStatusError => e 12 | Fog::Logger.warning ::JSON.parse(e.response.body)['error_msg'] 13 | raise e 14 | end 15 | 16 | private 17 | def parse(response) 18 | return response if response.body.empty? 19 | response.body = Fog::JSON.decode(response.body) 20 | response 21 | end 22 | end 23 | end 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/version.rb: -------------------------------------------------------------------------------- 1 | module Fog 2 | module Sakuracloud 3 | VERSION = "1.7.5" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /lib/fog/sakuracloud/volume.rb: -------------------------------------------------------------------------------- 1 | module Fog 2 | module Volume 3 | class SakuraCloud < Fog::Service 4 | requires :sakuracloud_api_token 5 | requires :sakuracloud_api_token_secret 6 | 7 | recognizes :sakuracloud_api_url, :api_zone 8 | 9 | model_path 'fog/sakuracloud/models/volume' 10 | model :archive 11 | collection :archives 12 | model :plan 13 | collection :plans 14 | model :disk 15 | collection :disks 16 | 17 | request_path 'fog/sakuracloud/requests/volume' 18 | request :list_disks 19 | request :list_plans 20 | request :create_disk 21 | request :configure_disk 22 | request :associate_ip_to_disk 23 | request :register_note_to_disk 24 | request :attach_disk 25 | request :delete_disk 26 | request :list_archives 27 | request :carve_hostname_on_disk 28 | 29 | class Real 30 | include Fog::SakuraCloud::Utils::Request 31 | 32 | def initialize(options = {}) 33 | @auth_encode = Base64.strict_encode64([ 34 | options[:sakuracloud_api_token], 35 | options[:sakuracloud_api_token_secret] 36 | ].join(':')) 37 | Fog.credentials[:sakuracloud_api_token] = options[:sakuracloud_api_token] 38 | Fog.credentials[:sakuracloud_api_token_secret] = options[:sakuracloud_api_token_secret] 39 | 40 | @sakuracloud_api_url = options[:sakuracloud_api_url] || 'https://secure.sakura.ad.jp' 41 | @api_zone = options[:api_zone] || Fog.credentials[:sakuracloud_api_zone] || 'is1b' 42 | Fog::SakuraCloud.validate_api_zone!(@api_zone) 43 | 44 | @connection = Fog::Core::Connection.new(@sakuracloud_api_url) 45 | end 46 | end 47 | 48 | class Mock 49 | def self.data 50 | @data ||= Hash.new do |hash, key| 51 | hash[key] = { 52 | :disks => [], 53 | :plans => [], 54 | :archives => [] 55 | } 56 | end 57 | end 58 | 59 | def self.reset 60 | @data = nil 61 | end 62 | 63 | def initialize(options={}) 64 | @sakuracloud_api_token = options[:sakuracloud_api_token] 65 | @sakuracloud_api_token_secret = options[:sakuracloud_api_token_secret] 66 | end 67 | 68 | def data 69 | self.class.data[@sakuracloud_api_token] 70 | self.class.data[@sakuracloud_api_token_secret] 71 | end 72 | 73 | def reset_data 74 | self.class.data.delete(@sakuracloud_api_token) 75 | self.class.data.delete(@sakuracloud_api_token_secret) 76 | end 77 | end 78 | end #SakuraCloud 79 | end #Volume 80 | end 81 | -------------------------------------------------------------------------------- /tests/helper.rb: -------------------------------------------------------------------------------- 1 | ## Initialize credentials 2 | ENV['FOG_RC'] = ENV['FOG_RC'] || File.expand_path('../.fog', __FILE__) 3 | 4 | ## From fog-core 5 | require 'fog/test_helpers' 6 | require 'fog/sakuracloud' 7 | 8 | if ENV['CI'] 9 | Fog.credentials[:sakuracloud_api_token] = 'dummy_token' 10 | Fog.credentials[:sakuracloud_api_token_secret] = 'dummy_secret' 11 | end 12 | 13 | ## SakuraCloud Helpers 14 | def sakuracloud_volume_service 15 | Fog::Volume[:sakuracloud] 16 | end 17 | 18 | def sakuracloud_compute_service 19 | Fog::Compute[:sakuracloud] 20 | end 21 | 22 | def sakuracloud_network_service 23 | Fog::Network[:sakuracloud] 24 | end 25 | -------------------------------------------------------------------------------- /tests/sakuracloud/requests/compute/plans_tests.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | Shindo.tests('Fog::Compute[:sakuracloud] | list_plans request', ['sakuracloud', 'compute']) do 3 | 4 | @plan_format = { 5 | 'Index' => Integer, 6 | 'ID' => Integer, 7 | 'Name' => String, 8 | 'CPU' => Integer, 9 | 'MemoryMB' => Integer, 10 | 'Availability' => String 11 | } 12 | 13 | tests('success') do 14 | 15 | tests('#list_plans') do 16 | serverplans = sakuracloud_compute_service.list_plans 17 | test 'returns a Hash' do 18 | serverplans.body.is_a? Hash 19 | end 20 | if Fog.mock? 21 | tests('ServerPlans').formats(@plan_format, false) do 22 | serverplans.body['ServerPlans'].first 23 | end 24 | else 25 | returns(200) { serverplans.status } 26 | returns(false) { serverplans.body.empty? } 27 | end 28 | end 29 | 30 | end 31 | 32 | end 33 | -------------------------------------------------------------------------------- /tests/sakuracloud/requests/compute/servers_tests.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | Shindo.tests('Fog::Compute[:sakuracloud] | list_servers request', ['sakuracloud', 'compute']) do 3 | 4 | @servers_format = { 5 | 'Index' => Integer, 6 | 'ID' => Integer, 7 | 'Name' => String, 8 | 'ServerPlan' => Hash, 9 | 'Instance' => Hash, 10 | 'Disks' => Array 11 | } 12 | 13 | tests('success') do 14 | 15 | tests('#list_servers') do 16 | servers = sakuracloud_compute_service.list_servers 17 | test 'returns a Hash' do 18 | servers.body.is_a? Hash 19 | end 20 | if Fog.mock? 21 | tests('Servers').formats(@servers_format, false) do 22 | servers.body['Servers'].first 23 | end 24 | else 25 | returns(200) { servers.status } 26 | returns(true) { servers.body.is_a? Hash } 27 | end 28 | end 29 | end 30 | end 31 | 32 | Shindo.tests('Fog::Compute[:sakuracloud] | create_servers request', ['sakuracloud', 'compute']) do 33 | tests('success') do 34 | tests('#create_servers') do 35 | servers = sakuracloud_compute_service.create_server(:name => 'foobar', :serverplan => 2001) 36 | test 'returns a Hash' do 37 | servers.body.is_a? Hash 38 | end 39 | 40 | unless Fog.mock? 41 | returns(201) { servers.status } 42 | returns(true) { servers.body.is_a? Hash } 43 | end 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /tests/sakuracloud/requests/compute/ssh_keys_tests.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | Shindo.tests('Fog::Compute[:sakuracloud] | list_ssh_keys request', ['sakuracloud', 'compute']) do 3 | 4 | @sshkey_format = { 5 | 'Index' => Integer, 6 | 'ID' => String, 7 | 'Name' => String, 8 | 'PublicKey' => String 9 | } 10 | 11 | tests('success') do 12 | 13 | tests('#list_ssh_keys') do 14 | sshkeys = sakuracloud_compute_service.list_ssh_keys 15 | test 'returns a Hash' do 16 | sshkeys.body.is_a? Hash 17 | end 18 | if Fog.mock? 19 | tests('SSHKeys').formats(@sshkey_format, false) do 20 | sshkeys.body['SSHKeys'].first 21 | end 22 | else 23 | returns(200) { sshkeys.status } 24 | returns(false) { sshkeys.body.empty? } 25 | end 26 | end 27 | 28 | end 29 | 30 | end 31 | -------------------------------------------------------------------------------- /tests/sakuracloud/requests/compute/zones_tests.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | Shindo.tests('Fog::Compute[:sakuracloud] | list_zones request', ['sakuracloud', 'compute']) do 3 | 4 | @zone_format = { 5 | 'Index' => Integer, 6 | 'ID' => Integer, 7 | 'Name' => String, 8 | 'Description' => String 9 | } 10 | 11 | tests('success') do 12 | 13 | tests('#list_zones') do 14 | zones = sakuracloud_compute_service.list_zones 15 | test 'returns a Hash' do 16 | zones.body.is_a? Hash 17 | end 18 | if Fog.mock? 19 | tests('Zones').formats(@zone_format, false) do 20 | zones.body['Zones'].first 21 | end 22 | else 23 | returns(200) { zones.status } 24 | returns(false) { zones.body.empty? } 25 | end 26 | end 27 | 28 | end 29 | 30 | end 31 | -------------------------------------------------------------------------------- /tests/sakuracloud/requests/network/routers_tests.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | Shindo.tests('Fog::Network[:sakuracloud] | list_routers request', ['sakuracloud', 'network']) do 3 | 4 | @routers_create_format = { 5 | 'Index' => Integer, 6 | 'ID' => String, 7 | 'Name' => String, 8 | 'BandWidthMbps' => Integer, 9 | 'NetworkMaskLen' => Integer, 10 | 'Switch' => Hash 11 | } 12 | 13 | @routers_list_format = { 14 | 'Index' => Integer, 15 | 'ID' => String, 16 | 'Switch' => Hash 17 | } 18 | 19 | tests('success') do 20 | 21 | tests('#list_routers') do 22 | routers = sakuracloud_network_service.list_routers 23 | test 'returns a Hash' do 24 | routers.body.is_a? Hash 25 | end 26 | if Fog.mock? 27 | tests('Routers').formats(@routers_list_format, false) do 28 | routers.body['Internet'].first 29 | end 30 | else 31 | returns(200) { routers.status } 32 | returns(true) { routers.body.is_a? Hash } 33 | end 34 | end 35 | end 36 | end 37 | 38 | Shindo.tests('Fog::Network[:sakuracloud] | create_router request', ['sakuracloud', 'network']) do 39 | tests('success') do 40 | tests('#create_router_with_internet_access') do 41 | router = sakuracloud_network_service.create_router(:name => 'foobar', :networkmasklen => 28) 42 | test 'returns a Hash' do 43 | router.body.is_a? Hash 44 | end 45 | 46 | unless Fog.mock? 47 | returns(202) { router.status } 48 | returns(true) { router.body.is_a? Hash } 49 | end 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /tests/sakuracloud/requests/network/switches_tests.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | Shindo.tests('Fog::Network[:sakuracloud] | list_switches request', ['sakuracloud', 'network']) do 3 | 4 | @switches_format = { 5 | 'Index' => Integer, 6 | 'ID' => String, 7 | 'Name' => String, 8 | 'ServerCount' => Integer, 9 | 'ApplianceCount' => Integer, 10 | 'Subnets' => Array 11 | } 12 | 13 | tests('success') do 14 | 15 | tests('#list_switches') do 16 | switches = sakuracloud_network_service.list_switches 17 | test 'returns a Hash' do 18 | switches.body.is_a? Hash 19 | end 20 | if Fog.mock? 21 | tests('Switches').formats(@switches_format, false) do 22 | switches.body['Switches'].first 23 | end 24 | else 25 | returns(200) { switches.status } 26 | returns(true) { switches.body.is_a? Hash } 27 | end 28 | end 29 | end 30 | end 31 | 32 | Shindo.tests('Fog::Network[:sakuracloud] | create_switch request', ['sakuracloud', 'network']) do 33 | tests('success') do 34 | tests('#create_simple_switch') do 35 | switch = sakuracloud_network_service.create_switch(:name => 'foobar') 36 | test 'returns a Hash' do 37 | switch.body.is_a? Hash 38 | end 39 | 40 | unless Fog.mock? 41 | returns(201) { switch.status } 42 | returns(true) { switch.body.is_a? Hash } 43 | end 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /tests/sakuracloud/requests/volume/archives_tests.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | Shindo.tests('Fog::Volume[:sakuracloud] | list_archives request', ['sakuracloud', 'volume']) do 3 | 4 | @archive_format = { 5 | 'Index' => Integer, 6 | 'ID' => Integer, 7 | 'Name' => String, 8 | 'SizeMB' => Integer, 9 | 'Plan' => Hash 10 | } 11 | 12 | tests('success') do 13 | 14 | tests('#list_archives') do 15 | archives = sakuracloud_volume_service.list_archives 16 | test 'returns a Hash' do 17 | archives.body.is_a? Hash 18 | end 19 | if Fog.mock? 20 | tests('Archives').formats(@archive_format, false) do 21 | archives.body['Archives'].first 22 | end 23 | else 24 | returns(200) { archives.status } 25 | returns(false) { archives.body.empty? } 26 | end 27 | end 28 | 29 | end 30 | 31 | end 32 | -------------------------------------------------------------------------------- /tests/sakuracloud/requests/volume/disks_tests.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | Shindo.tests('Fog::Volume[:sakuracloud] | list_disks request', ['sakuracloud', 'volume']) do 3 | 4 | @disks_format = { 5 | 'Index' => Integer, 6 | 'ID' => Integer, 7 | 'Name' => String, 8 | 'Connection' => String, 9 | 'Availability' => String, 10 | 'SizeMB' => Integer, 11 | 'Plan' => Hash 12 | } 13 | 14 | tests('success') do 15 | 16 | tests('#list_disks') do 17 | disks = sakuracloud_volume_service.list_disks 18 | test 'returns a Hash' do 19 | disks.body.is_a? Hash 20 | end 21 | if Fog.mock? 22 | tests('Disks').formats(@disks_format, false) do 23 | disks.body['Disks'].first 24 | end 25 | else 26 | returns(200) { disks.status } 27 | returns(true) { disks.body.is_a? Hash } 28 | end 29 | end 30 | end 31 | end 32 | 33 | Shindo.tests('Fog::Volume[:sakuracloud] | create_disks request', ['sakuracloud', 'volume']) do 34 | tests('success') do 35 | tests('#create_disks') do 36 | disks = sakuracloud_volume_service.create_disk('foobar', 4, {:sourcearchive => 112500463685}) 37 | test 'returns a Hash' do 38 | disks.body.is_a? Hash 39 | end 40 | 41 | unless Fog.mock? 42 | returns(202) { disks.status } 43 | returns(true) { disks.body.is_a? Hash } 44 | end 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /tests/sakuracloud/requests/volume/plans_tests.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | Shindo.tests('Fog::Volume[:sakuracloud] | list_plans request', ['sakuracloud', 'volume']) do 3 | 4 | @plan_format = { 5 | 'Index' => Integer, 6 | 'ID' => Integer, 7 | 'Name' => String, 8 | 'Availability' => String 9 | } 10 | 11 | tests('success') do 12 | 13 | tests('#list_plans') do 14 | diskplans = sakuracloud_volume_service.list_plans 15 | test 'returns a Hash' do 16 | diskplans.body.is_a? Hash 17 | end 18 | if Fog.mock? 19 | tests('DiskPlans').formats(@plan_format, false) do 20 | diskplans.body['DiskPlans'].first 21 | end 22 | else 23 | returns(200) { diskplans.status } 24 | returns(false) { diskplans.body.empty? } 25 | end 26 | end 27 | 28 | end 29 | 30 | end 31 | --------------------------------------------------------------------------------