Skip to content

Commit 364769f

Browse files
author
Dean Troyer
committed
Add compute keypair commands
Add create, delete, list, show keypair commands Part of blueprint nova-client Change-Id: Ieba00d3b3e3a326f875c01ac2a2b9bbd036cd7c9
1 parent 95bf187 commit 364769f

2 files changed

Lines changed: 169 additions & 0 deletions

File tree

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Copyright 2013 OpenStack Foundation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
#
15+
16+
"""Keypair action implementations"""
17+
18+
import logging
19+
import os
20+
import sys
21+
22+
from cliff import command
23+
from cliff import lister
24+
from cliff import show
25+
26+
from openstackclient.common import exceptions
27+
from openstackclient.common import utils
28+
29+
30+
class CreateKeypair(show.ShowOne):
31+
"""Create keypair command"""
32+
33+
api = "compute"
34+
log = logging.getLogger(__name__ + '.CreateKeypair')
35+
36+
def get_parser(self, prog_name):
37+
parser = super(CreateKeypair, self).get_parser(prog_name)
38+
parser.add_argument(
39+
'name',
40+
metavar='<name>',
41+
help='New keypair name',
42+
)
43+
parser.add_argument(
44+
'--public-key',
45+
metavar='<file>',
46+
help='Filename for public key to add',
47+
)
48+
return parser
49+
50+
def take_action(self, parsed_args):
51+
self.log.debug('take_action(%s)' % parsed_args)
52+
compute_client = self.app.client_manager.compute
53+
54+
public_key = parsed_args.public_key
55+
if public_key:
56+
try:
57+
with open(os.path.expanduser(parsed_args.public_key)) as p:
58+
public_key = p.read()
59+
except IOError as e:
60+
raise exceptions.CommandError(
61+
"Key file %s not found: %s" % (parsed_args.public_key, e))
62+
63+
keypair = compute_client.keypairs.create(
64+
parsed_args.name,
65+
public_key=public_key,
66+
)
67+
68+
# NOTE(dtroyer): how do we want to handle the display of the private
69+
# key when it needs to be communicated back to the user
70+
# For now, duplicate nova keypair-add command output
71+
info = {}
72+
if public_key:
73+
info.update(keypair._info)
74+
del info['public_key']
75+
return zip(*sorted(info.iteritems()))
76+
else:
77+
sys.stdout.write(keypair.private_key)
78+
return ({}, {})
79+
80+
81+
class DeleteKeypair(command.Command):
82+
"""Delete keypair command"""
83+
84+
api = "compute"
85+
log = logging.getLogger(__name__ + '.DeleteKeypair')
86+
87+
def get_parser(self, prog_name):
88+
parser = super(DeleteKeypair, self).get_parser(prog_name)
89+
parser.add_argument(
90+
'name',
91+
metavar='<name>',
92+
help='Name of keypair to delete',
93+
)
94+
return parser
95+
96+
def take_action(self, parsed_args):
97+
self.log.debug('take_action(%s)' % parsed_args)
98+
compute_client = self.app.client_manager.compute
99+
compute_client.keypairs.delete(parsed_args.name)
100+
return
101+
102+
103+
class ListKeypair(lister.Lister):
104+
"""List keypair command"""
105+
106+
api = "compute"
107+
log = logging.getLogger(__name__ + ".ListKeypair")
108+
109+
def take_action(self, parsed_args):
110+
self.log.debug("take_action(%s)" % parsed_args)
111+
compute_client = self.app.client_manager.compute
112+
columns = (
113+
"Name",
114+
"Fingerprint"
115+
)
116+
data = compute_client.keypairs.list()
117+
118+
return (columns,
119+
(utils.get_item_properties(
120+
s, columns,
121+
) for s in data))
122+
123+
124+
class ShowKeypair(show.ShowOne):
125+
"""Show keypair command"""
126+
127+
api = 'compute'
128+
log = logging.getLogger(__name__ + '.ShowKeypair')
129+
130+
def get_parser(self, prog_name):
131+
parser = super(ShowKeypair, self).get_parser(prog_name)
132+
parser.add_argument(
133+
'name',
134+
metavar='<name>',
135+
help='Name of keypair to display',
136+
)
137+
parser.add_argument(
138+
'--public-key',
139+
action='store_true',
140+
default=False,
141+
help='Include public key in output',
142+
)
143+
return parser
144+
145+
def take_action(self, parsed_args):
146+
self.log.debug('take_action(%s)' % parsed_args)
147+
compute_client = self.app.client_manager.compute
148+
keypair = utils.find_resource(compute_client.keypairs,
149+
parsed_args.name)
150+
151+
info = {}
152+
info.update(keypair._info['keypair'])
153+
if not parsed_args.public_key:
154+
del info['public_key']
155+
return zip(*sorted(info.iteritems()))
156+
else:
157+
# NOTE(dtroyer): a way to get the public key in a similar form
158+
# as the private key in the create command
159+
sys.stdout.write(keypair.public_key)
160+
return ({}, {})

setup.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,15 @@ def read(fname):
202202
'show_hypervisor='
203203
'openstackclient.compute.v2.hypervisor:ShowHypervisor',
204204

205+
'create_keypair='
206+
'openstackclient.compute.v2.keypair:CreateKeypair',
207+
'delete_keypair='
208+
'openstackclient.compute.v2.keypair:DeleteKeypair',
209+
'list_keypair='
210+
'openstackclient.compute.v2.keypair:ListKeypair',
211+
'show_keypair='
212+
'openstackclient.compute.v2.keypair:ShowKeypair',
213+
205214
'create_server=openstackclient.compute.v2.server:CreateServer',
206215
'delete_server=openstackclient.compute.v2.server:DeleteServer',
207216
'list_server=openstackclient.compute.v2.server:ListServer',

0 commit comments

Comments
 (0)