| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
"""Orchestration v1 software config action implementations""" |
| 15 |
|
| 16 |
import logging |
| 17 |
|
| 18 |
from osc_lib.command import command |
| 19 |
from osc_lib import exceptions as exc |
| 20 |
from osc_lib import utils |
| 21 |
from urllib import request |
| 22 |
import yaml |
| 23 |
|
| 24 |
from heatclient._i18n import _ |
| 25 |
from heatclient.common import format_utils |
| 26 |
from heatclient.common import template_format |
| 27 |
from heatclient.common import utils as heat_utils |
| 28 |
from heatclient import exc as heat_exc |
| 29 |
|
| 30 |
|
| 31 |
class DeleteConfig(command.Command): |
| 32 |
"""Delete software configs""" |
| 33 |
|
| 34 |
log = logging.getLogger(__name__ + ".DeleteConfig") |
| 35 |
|
| 36 |
def get_parser(self, prog_name): |
| 37 |
parser = super().get_parser(prog_name) |
| 38 |
parser.add_argument( |
| 39 |
'config', |
| 40 |
metavar='<config>', |
| 41 |
nargs='+', |
| 42 |
help=_('IDs of the software configs to delete') |
| 43 |
) |
| 44 |
return parser |
| 45 |
|
| 46 |
def take_action(self, parsed_args): |
| 47 |
self.log.debug("take_action(%s)", parsed_args) |
| 48 |
|
| 49 |
heat_client = self.app.client_manager.orchestration |
| 50 |
return _delete_config(heat_client, parsed_args) |
| 51 |
|
| 52 |
|
| 53 |
def _delete_config(heat_client, args): |
| 54 |
failure_count = 0 |
| 55 |
|
| 56 |
for config_id in args.config: |
| 57 |
try: |
| 58 |
heat_client.software_configs.delete( |
| 59 |
config_id=config_id) |
| 60 |
except Exception as e: |
| 61 |
if isinstance(e, heat_exc.HTTPNotFound): |
| 62 |
print(_('Software config with ID %s not found') % config_id) |
| 63 |
failure_count += 1 |
| 64 |
continue |
| 65 |
|
| 66 |
if failure_count: |
| 67 |
raise exc.CommandError(_('Unable to delete %(count)s of the ' |
| 68 |
'%(total)s software configs.') % |
| 69 |
{'count': failure_count, |
| 70 |
'total': len(args.config)}) |
| 71 |
|
| 72 |
|
| 73 |
class ListConfig(command.Lister): |
| 74 |
"""List software configs""" |
| 75 |
|
| 76 |
log = logging.getLogger(__name__ + ".ListConfig") |
| 77 |
|
| 78 |
def get_parser(self, prog_name): |
| 79 |
parser = super().get_parser(prog_name) |
| 80 |
parser.add_argument( |
| 81 |
'--limit', |
| 82 |
metavar='<limit>', |
| 83 |
type=int, |
| 84 |
help=_('Limit the number of configs returned') |
| 85 |
) |
| 86 |
parser.add_argument( |
| 87 |
'--marker', |
| 88 |
metavar='<id>', |
| 89 |
help=_('Return configs that appear after the given config ID') |
| 90 |
) |
| 91 |
return parser |
| 92 |
|
| 93 |
def take_action(self, parsed_args): |
| 94 |
self.log.debug("take_action(%s)", parsed_args) |
| 95 |
heat_client = self.app.client_manager.orchestration |
| 96 |
return _list_config(heat_client, parsed_args) |
| 97 |
|
| 98 |
|
| 99 |
def _list_config(heat_client, args): |
| 100 |
kwargs = {} |
| 101 |
if args.limit: |
| 102 |
kwargs['limit'] = args.limit |
| 103 |
if args.marker: |
| 104 |
kwargs['marker'] = args.marker |
| 105 |
scs = heat_client.software_configs.list(**kwargs) |
| 106 |
|
| 107 |
columns = ['id', 'name', 'group', 'creation_time'] |
| 108 |
return (columns, (utils.get_item_properties(s, columns) for s in scs)) |
| 109 |
|
| 110 |
|
| 111 |
class CreateConfig(format_utils.JsonFormat): |
| 112 |
"""Create software config""" |
| 113 |
|
| 114 |
log = logging.getLogger(__name__ + ".CreateConfig") |
| 115 |
|
| 116 |
def get_parser(self, prog_name): |
| 117 |
parser = super().get_parser(prog_name) |
| 118 |
parser.add_argument( |
| 119 |
'name', |
| 120 |
metavar='<config-name>', |
| 121 |
help=_('Name of the software config to create') |
| 122 |
) |
| 123 |
parser.add_argument( |
| 124 |
'--config-file', |
| 125 |
metavar='<config-file>', |
| 126 |
help=_('Path to JSON/YAML containing map defining ' |
| 127 |
'<inputs>, <outputs>, and <options>') |
| 128 |
) |
| 129 |
parser.add_argument( |
| 130 |
'--definition-file', |
| 131 |
metavar='<destination-file>', |
| 132 |
help=_('Path to software config script/data') |
| 133 |
) |
| 134 |
parser.add_argument( |
| 135 |
'--group', |
| 136 |
metavar='<group>', |
| 137 |
default='Heat::Ungrouped', |
| 138 |
help=_('Group name of tool expected by the software config') |
| 139 |
) |
| 140 |
return parser |
| 141 |
|
| 142 |
def take_action(self, parsed_args): |
| 143 |
self.log.debug("take_action(%s)", parsed_args) |
| 144 |
heat_client = self.app.client_manager.orchestration |
| 145 |
return _create_config(heat_client, parsed_args) |
| 146 |
|
| 147 |
|
| 148 |
def _create_config(heat_client, args): |
| 149 |
config = { |
| 150 |
'group': args.group, |
| 151 |
'config': '' |
| 152 |
} |
| 153 |
|
| 154 |
defn = {} |
| 155 |
if args.definition_file: |
| 156 |
defn_url = heat_utils.normalise_file_path_to_url( |
| 157 |
args.definition_file) |
| 158 |
defn_raw = request.urlopen(defn_url).read() or '{}' |
| 159 |
defn = yaml.load(defn_raw, Loader=template_format.yaml_loader) |
| 160 |
|
| 161 |
config['inputs'] = defn.get('inputs', []) |
| 162 |
config['outputs'] = defn.get('outputs', []) |
| 163 |
config['options'] = defn.get('options', {}) |
| 164 |
|
| 165 |
if args.config_file: |
| 166 |
config_url = heat_utils.normalise_file_path_to_url( |
| 167 |
args.config_file) |
| 168 |
config['config'] = request.urlopen(config_url).read() |
| 169 |
|
| 170 |
|
| 171 |
validate_template = { |
| 172 |
'heat_template_version': '2013-05-23', |
| 173 |
'resources': { |
| 174 |
args.name: { |
| 175 |
'type': 'OS::Heat::SoftwareConfig', |
| 176 |
'properties': config |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
heat_client.stacks.validate(template=validate_template) |
| 181 |
|
| 182 |
config['name'] = args.name |
| 183 |
sc = heat_client.software_configs.create(**config).to_dict() |
| 184 |
rows = list(sc.values()) |
| 185 |
columns = list(sc.keys()) |
| 186 |
return columns, rows |
| 187 |
|
| 188 |
|
| 189 |
class ShowConfig(format_utils.YamlFormat): |
| 190 |
"""Show software config details""" |
| 191 |
|
| 192 |
log = logging.getLogger(__name__ + ".ShowConfig") |
| 193 |
|
| 194 |
def get_parser(self, prog_name): |
| 195 |
parser = super().get_parser(prog_name) |
| 196 |
parser.add_argument( |
| 197 |
'config', |
| 198 |
metavar='<config>', |
| 199 |
help=_('ID of the config') |
| 200 |
) |
| 201 |
parser.add_argument( |
| 202 |
'--config-only', |
| 203 |
default=False, |
| 204 |
action="store_true", |
| 205 |
help=_('Only display the value of the <config> property.') |
| 206 |
) |
| 207 |
return parser |
| 208 |
|
| 209 |
def take_action(self, parsed_args): |
| 210 |
self.log.debug("take_action(%s)", parsed_args) |
| 211 |
heat_client = self.app.client_manager.orchestration |
| 212 |
return _show_config(heat_client, config_id=parsed_args.config, |
| 213 |
config_only=parsed_args.config_only) |
| 214 |
|
| 215 |
|
| 216 |
def _show_config(heat_client, config_id, config_only): |
| 217 |
try: |
| 218 |
sc = heat_client.software_configs.get(config_id=config_id) |
| 219 |
except heat_exc.HTTPNotFound: |
| 220 |
raise exc.CommandError(_('Configuration not found: %s') % config_id) |
| 221 |
|
| 222 |
columns = None |
| 223 |
rows = None |
| 224 |
|
| 225 |
if config_only: |
| 226 |
print(sc.config) |
| 227 |
else: |
| 228 |
columns = ( |
| 229 |
'id', |
| 230 |
'name', |
| 231 |
'group', |
| 232 |
'config', |
| 233 |
'inputs', |
| 234 |
'outputs', |
| 235 |
'options', |
| 236 |
'creation_time', |
| 237 |
) |
| 238 |
rows = utils.get_dict_properties(sc.to_dict(), columns) |
| 239 |
|
| 240 |
return columns, rows |