Debian Codegraph @

neutronclient/neutron/v2_0/flavor/flavor.py

raw
1 # Copyright 2015 Hewlett-Packard Development Company, L.P.
2 # All Rights Reserved
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import argparse
17
18 from neutronclient._i18n import _
19 from neutronclient.common import utils
20 from neutronclient.neutron import v2_0 as neutronV20
21
22
23 class ListFlavor(neutronV20.ListCommand):
24 """List Neutron service flavors."""
25
26 resource = 'flavor'
27 list_columns = ['id', 'name', 'service_type', 'enabled']
28 pagination_support = True
29 sorting_support = True
30
31
32 class ShowFlavor(neutronV20.ShowCommand):
33 """Show information about a given Neutron service flavor."""
34
35 resource = 'flavor'
36
37
38 class CreateFlavor(neutronV20.CreateCommand):
39 """Create a Neutron service flavor."""
40
41 resource = 'flavor'
42
43 def add_known_arguments(self, parser):
44 parser.add_argument(
45 'name',
46 metavar='NAME',
47 help=_('Name for the flavor.'))
48 parser.add_argument(
49 'service_type',
50 metavar='SERVICE_TYPE',
51 help=_('Service type to which the flavor applies to: e.g. VPN. '
52 '(See service-provider-list for loaded examples.)'))
53 parser.add_argument(
54 '--description',
55 help=_('Description for the flavor.'))
56 utils.add_boolean_argument(
57 parser,
58 '--enabled',
59 default=argparse.SUPPRESS,
60 help=_('Sets enabled flag.'))
61
62 def args2body(self, parsed_args):
63 body = {}
64 neutronV20.update_dict(parsed_args, body,
65 ['name', 'description', 'service_type',
66 'enabled'])
67 return {self.resource: body}
68
69
70 class DeleteFlavor(neutronV20.DeleteCommand):
71 """Delete a given Neutron service flavor."""
72
73 resource = 'flavor'
74
75
76 class UpdateFlavor(neutronV20.UpdateCommand):
77 """Update a Neutron service flavor."""
78
79 resource = 'flavor'
80
81 def add_known_arguments(self, parser):
82 parser.add_argument(
83 '--name',
84 help=_('Name for the flavor.'))
85 parser.add_argument(
86 '--description',
87 help=_('Description for the flavor.'))
88 utils.add_boolean_argument(
89 parser,
90 '--enabled',
91 default=argparse.SUPPRESS,
92 help=_('Sets enabled flag.'))
93
94 def args2body(self, parsed_args):
95 body = {}
96 neutronV20.update_dict(parsed_args, body,
97 ['name', 'description', 'enabled'])
98 return {self.resource: body}
99
100
101 class AssociateFlavor(neutronV20.NeutronCommand):
102 """Associate a Neutron service flavor with a flavor profile."""
103
104 resource = 'flavor'
105
106 def get_parser(self, prog_name):
107 parser = super(AssociateFlavor, self).get_parser(prog_name)
108 parser.add_argument(
109 'flavor',
110 metavar='FLAVOR',
111 help=_('ID or name of the flavor to associate.'))
112 parser.add_argument(
113 'flavor_profile',
114 metavar='FLAVOR_PROFILE',
115 help=_('ID of the flavor profile to be associated with the '
116 'flavor.'))
117 return parser
118
119 def take_action(self, parsed_args):
120 neutron_client = self.get_client()
121 flavor_id = neutronV20.find_resourceid_by_name_or_id(
122 neutron_client, 'flavor', parsed_args.flavor)
123 service_profile_id = neutronV20.find_resourceid_by_id(
124 neutron_client, 'service_profile', parsed_args.flavor_profile)
125 body = {'service_profile': {'id': service_profile_id}}
126 neutron_client.associate_flavor(flavor_id, body)
127 print((_('Associated flavor %(flavor)s with '
128 'flavor_profile %(profile)s') %
129 {'flavor': parsed_args.flavor,
130 'profile': parsed_args.flavor_profile}),
131 file=self.app.stdout)
132
133
134 class DisassociateFlavor(neutronV20.NeutronCommand):
135 """Disassociate a Neutron service flavor from a flavor profile."""
136
137 resource = 'flavor'
138
139 def get_parser(self, prog_name):
140 parser = super(DisassociateFlavor, self).get_parser(prog_name)
141 parser.add_argument(
142 'flavor',
143 metavar='FLAVOR',
144 help=_('ID or name of the flavor to be disassociated.'))
145 parser.add_argument(
146 'flavor_profile',
147 metavar='FLAVOR_PROFILE',
148 help=_('ID of the flavor profile to be disassociated from the '
149 'flavor.'))
150 return parser
151
152 def take_action(self, parsed_args):
153 neutron_client = self.get_client()
154 flavor_id = neutronV20.find_resourceid_by_name_or_id(
155 neutron_client, 'flavor', parsed_args.flavor)
156 service_profile_id = neutronV20.find_resourceid_by_id(
157 neutron_client, 'service_profile', parsed_args.flavor_profile)
158 neutron_client.disassociate_flavor(flavor_id, service_profile_id)
159 print((_('Disassociated flavor %(flavor)s from '
160 'flavor_profile %(profile)s') %
161 {'flavor': parsed_args.flavor,
162 'profile': parsed_args.flavor_profile}),
163 file=self.app.stdout)