Class: Chef::Provider::GoogleKeyPair

Inherits:
LWRPBase
  • Object
show all
Defined in:
lib/chef/provider/google_key_pair.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_fingerprintObject (readonly)

Returns the value of attribute current_fingerprint.



63
64
65
# File 'lib/chef/provider/google_key_pair.rb', line 63

def current_fingerprint
  @current_fingerprint
end

Instance Method Details

#action_handlerObject

TODO abstract into base class



162
163
164
# File 'lib/chef/provider/google_key_pair.rb', line 162

def action_handler
  @action_handler ||= Chef::Provisioning::ChefProviderActionHandler.new(self)
end

#allow_overwriteObject



108
109
110
# File 'lib/chef/provider/google_key_pair.rb', line 108

def allow_overwrite
  new_resource.allow_overwrite
end

#converge_local_keys(action) ⇒ Object

Converge the local keys private_key_path will always be populated but public_key_path can be parsed from the private key later if it isn’t provided



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef/provider/google_key_pair.rb', line 93

def converge_local_keys(action)
  resource = new_resource
  Cheffish.inline_resource(self, action) do
    directory run_context.config[:private_key_write_path]
    private_key resource.private_key_path do
      public_key_path resource.public_key_path if resource.public_key_path
      if resource.private_key_options
        resource.private_key_options.each_pair do |key, value|
          send(key, value)
        end
      end
    end
  end
end

#current_resource_exists?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/chef/provider/google_key_pair.rb', line 86

def current_resource_exists?
  @current_resource.action != [ :destroy ]
end

#desired_keyObject



127
128
129
130
131
132
133
134
135
136
# File 'lib/chef/provider/google_key_pair.rb', line 127

def desired_key
  @desired_public_key ||= begin
    if new_public_key_path
      public_key, format = Cheffish::KeyFormatter.decode(IO.read(new_public_key_path))
      public_key
    else
      desired_private_key.public_key
    end
  end
end

#desired_key_opensshObject



138
139
140
# File 'lib/chef/provider/google_key_pair.rb', line 138

def desired_key_openssh
  Cheffish::KeyFormatter.encode(desired_key, :format => :openssh)
end

#desired_private_keyObject



120
121
122
123
124
125
# File 'lib/chef/provider/google_key_pair.rb', line 120

def desired_private_key
  @desired_private_key ||= begin
    private_key, format = Cheffish::KeyFormatter.decode(IO.read(new_private_key_path))
    private_key
  end
end

#driverObject

TODO abstract into base class



157
158
159
# File 'lib/chef/provider/google_key_pair.rb', line 157

def driver
  new_resource.driver
end

#load_current_resourceObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chef/provider/google_key_pair.rb', line 65

def load_current_resource
  @current_resource = Chef::Resource::GoogleKeyPair.new(new_resource.name, run_context)

  existing_key = driver.project_client.get.ssh_mappings[new_resource.name]
  if existing_key
    @current_fingerprint = Cheffish::KeyFormatter.encode(
      Cheffish::KeyFormatter.decode(existing_key)[0],
      :format => :fingerprint
    )
  else
    current_resource.action :destroy
  end

  if new_private_key_path && ::File.exist?(new_private_key_path)
    current_resource.private_key_path new_private_key_path
  end
  if new_public_key_path && ::File.exist?(new_public_key_path)
    current_resource.public_key_path new_public_key_path
  end
end

#new_private_key_pathObject



112
113
114
# File 'lib/chef/provider/google_key_pair.rb', line 112

def new_private_key_path
  new_resource.private_key_path
end

#new_public_key_pathObject



116
117
118
# File 'lib/chef/provider/google_key_pair.rb', line 116

def new_public_key_path
  new_resource.public_key_path
end

#reupload_metadata(metadata) ⇒ Object



166
167
168
169
170
171
# File 'lib/chef/provider/google_key_pair.rb', line 166

def ()
  converge_by "reuploading changed metadata" do
    operation = driver.project_client.()
    driver.global_operations_client.wait_for_done(action_handler, operation)
  end
end

#set_key_and_mappingObject



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/chef/provider/google_key_pair.rb', line 142

def set_key_and_mapping
  username = desired_key_openssh.split(" ")[2].split("@")[0]
   = driver.project_client.get
  converge_by "adding key for username #{username}" do
    .ensure_key(username, desired_key_openssh)
  end

  converge_by "ensuring we store metadata mapping for google_key_pair[#{new_resource.name}]" do
    .set_ssh_mapping(new_resource.name, desired_key_openssh)
  end

  ()
end