Class: Chef::Provisioning::GoogleDriver::Client::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/provisioning/google_driver/client/metadata.rb

Overview

Wraps a response of a projects.get request to the GCE API and provides access to metadata like SSH keys. It can also be modified and passed back to the projects client which will set the new metadata by sending a projects.set_common_instance_metadata request to the GCE API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Metadata

Returns a new instance of Metadata.



11
12
13
14
15
16
17
# File 'lib/chef/provisioning/google_driver/client/metadata.rb', line 11

def initialize(response)
   = response[:body][:commonInstanceMetadata]
  @fingerprint = [:fingerprint]
  @items = [:items].dup || []
  sort_items!
  @changed = false
end

Instance Attribute Details

#fingerprintObject (readonly)

Returns the value of attribute fingerprint.



19
20
21
# File 'lib/chef/provisioning/google_driver/client/metadata.rb', line 19

def fingerprint
  @fingerprint
end

#itemsObject (readonly)

Returns the value of attribute items.



19
20
21
# File 'lib/chef/provisioning/google_driver/client/metadata.rb', line 19

def items
  @items
end

Instance Method Details

#changed?Boolean

Returns true iff the metadata has been changed since it has been retrieved from GCE.

Returns:

  • (Boolean)


23
24
25
# File 'lib/chef/provisioning/google_driver/client/metadata.rb', line 23

def changed?
  @changed
end

#delete_ssh_key(value) ⇒ Object

Deletes an ssh key by value, i.e. by the SSH key.



43
44
45
46
# File 'lib/chef/provisioning/google_driver/client/metadata.rb', line 43

def delete_ssh_key(value)
  ssh_keys.reject! { |k, v| v == value }
  (SSH_KEYS, serialize_keys(ssh_keys))
end

#delete_ssh_mapping(name) ⇒ Object



63
64
65
66
# File 'lib/chef/provisioning/google_driver/client/metadata.rb', line 63

def delete_ssh_mapping(name)
  ssh_mappings.delete(name)
  (SSH_MAPPINGS, serialize_keys(ssh_mappings))
end

#ensure_key(username, local_key) ⇒ Object

Ensure the list of keys stored in GCE contains the provided key. If the provided key already exists it will be updated TODO: This will need to be updated later with user accounts: see cloud.google.com/compute/docs/access/user-accounts/



37
38
39
40
# File 'lib/chef/provisioning/google_driver/client/metadata.rb', line 37

def ensure_key(username, local_key)
  ssh_keys << [username, local_key]
  (SSH_KEYS, serialize_keys(ssh_keys))
end

#set_ssh_mapping(name, local_key) ⇒ Object



56
57
58
59
60
61
# File 'lib/chef/provisioning/google_driver/client/metadata.rb', line 56

def set_ssh_mapping(name, local_key)
  # TODO what if users change the name of the resource but not the key
  # should we remove the old entry from both mappings?
  ssh_mappings[name] = local_key
  (SSH_MAPPINGS, serialize_keys(ssh_mappings))
end

#ssh_keysObject

Note that the returned object should not be modified directly. Instead, set_ssh_key or delete_ssh_key should be used.



29
30
31
# File 'lib/chef/provisioning/google_driver/client/metadata.rb', line 29

def ssh_keys
  @ssh_keys ||= parse_keys((SSH_KEYS))
end

#ssh_mappingsObject

We store the mapping of resource name to key value in GCE so we can tell if a key has changed. Note that the returned object should not be modified directly. Instead, set_ssh_mapping or delete_ssh_mapping should be used.



52
53
54
# File 'lib/chef/provisioning/google_driver/client/metadata.rb', line 52

def ssh_mappings
  @ssh_mappings ||= Hash[parse_keys((SSH_MAPPINGS))]
end