Class: Milc::Gcloud::Resource
- Inherits:
-
Object
- Object
- Milc::Gcloud::Resource
- Defined in:
- lib/milc/gcloud/resource.rb
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Class Method Summary collapse
- .command_def_for(service, resource) ⇒ Object
- .command_defs ⇒ Object
- .lookup(project, service, resource) ⇒ Object
Instance Method Summary collapse
- #__gcloud(cmd, options = {}, &block) ⇒ Object
- #build_attr_arg(attr_name, value) ⇒ Object
- #build_attr_args(attrs) ⇒ Object
- #build_sub_attr_args(attrs) ⇒ Object
- #call_action(action, cmd_args, attrs = nil, &block) ⇒ Object
- #call_update(cmd_args, attrs, &block) ⇒ Object
- #compare(attrs, res) ⇒ Object
- #create(cmd_args, attrs, &block) ⇒ Object
- #delete(cmd_args, attrs = {}, &block) ⇒ Object
- #find(name) ⇒ Object
-
#initialize(project, service, resource, commands) ⇒ Resource
constructor
A new instance of Resource.
- #normalize_keys(obj) ⇒ Object
- #raise_if_invalid(command) ⇒ Object
- #split_sort(str, spliter = /\s+/, &block) ⇒ Object
- #update(cmd_args, attrs, &block) ⇒ Object
Constructor Details
#initialize(project, service, resource, commands) ⇒ Resource
Returns a new instance of Resource.
46 47 48 49 |
# File 'lib/milc/gcloud/resource.rb', line 46 def initialize(project, service, resource, commands) @project, @service, @resource = project, service, resource @commands = commands || [] end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
45 46 47 |
# File 'lib/milc/gcloud/resource.rb', line 45 def commands @commands end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
45 46 47 |
# File 'lib/milc/gcloud/resource.rb', line 45 def project @project end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
45 46 47 |
# File 'lib/milc/gcloud/resource.rb', line 45 def resource @resource end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
45 46 47 |
# File 'lib/milc/gcloud/resource.rb', line 45 def service @service end |
Class Method Details
.command_def_for(service, resource) ⇒ Object
18 19 20 21 22 |
# File 'lib/milc/gcloud/resource.rb', line 18 def command_def_for(service, resource) c = command_defs s = c[service.to_s] or raise "service #{service.inspect} not found in #{c.keys.join(', ')}" s[resource.to_s] or raise "resource #{resource.inspect} not found in #{s.keys.join(' ')} of #{service.inspect}" end |
.command_defs ⇒ Object
14 15 16 |
# File 'lib/milc/gcloud/resource.rb', line 14 def command_defs @command_defs ||= YAML.load_file(File.("../commands.yml", __FILE__)) end |
.lookup(project, service, resource) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/milc/gcloud/resource.rb', line 24 def lookup(project, service, resource) Milc.logger.debug("=" * 100) Milc.logger.debug("project: #{project}, service: #{service}, resource: #{resource}") service = service.to_s resource = resource.to_s command_def = command_def_for(service, resource) result = new(project, service, resource, command_def['commands']) service_module = Milc::Gcloud.const_get(service.classify) m = resource.gsub(/-/, '_').camelize.to_sym if service_module.constants.include?(m) resource_module = service_module.const_get(m) Milc.logger.debug("#{m} found as #{resource_module.inspect}") result.extend(resource_module) else Milc.logger.debug("#{m} not found in #{service_module.constants.inspect}") end result end |
Instance Method Details
#__gcloud(cmd, options = {}, &block) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/milc/gcloud/resource.rb', line 51 def __gcloud(cmd, = {}, &block) c = "gcloud #{cmd} --format json" c << " --project #{project}" unless c =~ /\s\-\-project[\s\=]/ res = Gcloud.backend.execute(c, , &block) res ? JSON.parse(res) : nil end |
#build_attr_arg(attr_name, value) ⇒ Object
58 59 60 |
# File 'lib/milc/gcloud/resource.rb', line 58 def build_attr_arg(attr_name, value) "--#{attr_name.to_s.gsub(/\_/, '-')} #{value}" end |
#build_attr_args(attrs) ⇒ Object
62 63 64 |
# File 'lib/milc/gcloud/resource.rb', line 62 def build_attr_args(attrs) attrs.map{|k,v| build_attr_arg(k,v) }.join(" ") end |
#build_sub_attr_args(attrs) ⇒ Object
66 67 68 |
# File 'lib/milc/gcloud/resource.rb', line 66 def build_sub_attr_args(attrs) attrs.map{|k,v| "#{k.to_s.gsub(/\_/, '-')}=#{v}" }.join(" ") end |
#call_action(action, cmd_args, attrs = nil, &block) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/milc/gcloud/resource.rb', line 70 def call_action(action, cmd_args, attrs = nil, &block) attr_args = attrs.nil? ? '' : build_attr_args(attrs) = action =~ /\Alist|\Adescribe|\Aget/ ? {returns: :stdout, logging: :stderr} : {returns: :none , logging: :both} __gcloud("#{service} #{resource} #{action} #{cmd_args} #{attr_args}", , &block) end |
#call_update(cmd_args, attrs, &block) ⇒ Object
79 80 81 |
# File 'lib/milc/gcloud/resource.rb', line 79 def call_update(cmd_args, attrs, &block) call_action(:update, cmd_args, attrs, &block) end |
#compare(attrs, res) ⇒ Object
105 106 107 108 109 |
# File 'lib/milc/gcloud/resource.rb', line 105 def compare(attrs, res) Milc.logger.debug("compare\n attrs: #{attrs.inspect}\n res: #{res.inspect}") res = normalize_keys(res) attrs.all?{|k,v| res[k.to_s.gsub(/-/, '_').to_sym] == v } end |
#create(cmd_args, attrs, &block) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/milc/gcloud/resource.rb', line 116 def create(cmd_args, attrs, &block) raise_if_invalid(:create) name, args = *cmd_args.split(/\s+/, 2) r = find(name) if r return r unless commands.include?('update') if compare(attrs, r) return r else return call_update(cmd_args, attrs, &block) end else call_action(:create, cmd_args, attrs, &block) end end |
#delete(cmd_args, attrs = {}, &block) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/milc/gcloud/resource.rb', line 141 def delete(cmd_args, attrs = {}, &block) raise_if_invalid(:delete) name, args = *cmd_args.split(/\s+/, 2) r = find(name) return nil unless r call_action(:delete, cmd_args, attrs, &block) end |
#find(name) ⇒ Object
83 84 85 86 |
# File 'lib/milc/gcloud/resource.rb', line 83 def find(name) r = call_action(:list, name) r ? r.first : nil end |
#normalize_keys(obj) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/milc/gcloud/resource.rb', line 93 def normalize_keys(obj) case obj when Array then obj.map{|o| normalize_keys(o) } when Hash then obj.each_with_object({}) do |(k,v), d| d[k.to_s.underscore.to_sym] = normalize_keys(v) end else obj end end |
#raise_if_invalid(command) ⇒ Object
88 89 90 91 |
# File 'lib/milc/gcloud/resource.rb', line 88 def raise_if_invalid(command) return if commands.include?(command.to_s) raise NotImplementedError, "#{service} #{resource} #{command} is not supported." end |
#split_sort(str, spliter = /\s+/, &block) ⇒ Object
111 112 113 114 |
# File 'lib/milc/gcloud/resource.rb', line 111 def split_sort(str, spliter = /\s+/, &block) return nil unless str str.split(spliter).sort(&block) end |
#update(cmd_args, attrs, &block) ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'lib/milc/gcloud/resource.rb', line 132 def update(cmd_args, attrs, &block) raise_if_invalid(:update) name, args = *cmd_args.split(/\s+/, 2) r = find(name) raise "Resource not found #{service} #{resource} #{name}" unless r return r if compare(attrs, r) call_update(cmd_args, attrs, &block) end |