|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +# |
| 13 | + |
| 14 | +"""Compute v2 API Library""" |
| 15 | + |
| 16 | +from keystoneauth1 import exceptions as ksa_exceptions |
| 17 | +from osc_lib.api import api |
| 18 | +from osc_lib import exceptions |
| 19 | +from osc_lib.i18n import _ |
| 20 | + |
| 21 | + |
| 22 | +class APIv2(api.BaseAPI): |
| 23 | + """Compute v2 API""" |
| 24 | + |
| 25 | + def __init__(self, **kwargs): |
| 26 | + super(APIv2, self).__init__(**kwargs) |
| 27 | + |
| 28 | + # Overrides |
| 29 | + |
| 30 | + # TODO(dtroyer): Override find() until these fixes get into an osc-lib |
| 31 | + # minimum release |
| 32 | + def find( |
| 33 | + self, |
| 34 | + path, |
| 35 | + value=None, |
| 36 | + attr=None, |
| 37 | + ): |
| 38 | + """Find a single resource by name or ID |
| 39 | +
|
| 40 | + :param string path: |
| 41 | + The API-specific portion of the URL path |
| 42 | + :param string value: |
| 43 | + search expression (required, really) |
| 44 | + :param string attr: |
| 45 | + name of attribute for secondary search |
| 46 | + """ |
| 47 | + |
| 48 | + try: |
| 49 | + ret = self._request('GET', "/%s/%s" % (path, value)).json() |
| 50 | + if isinstance(ret, dict): |
| 51 | + # strip off the enclosing dict |
| 52 | + key = list(ret.keys())[0] |
| 53 | + ret = ret[key] |
| 54 | + except ( |
| 55 | + ksa_exceptions.NotFound, |
| 56 | + ksa_exceptions.BadRequest, |
| 57 | + ): |
| 58 | + kwargs = {attr: value} |
| 59 | + try: |
| 60 | + ret = self.find_one(path, **kwargs) |
| 61 | + except ksa_exceptions.NotFound: |
| 62 | + msg = _("%s not found") % value |
| 63 | + raise exceptions.NotFound(msg) |
| 64 | + |
| 65 | + return ret |
| 66 | + |
| 67 | + # Security Groups |
| 68 | + |
| 69 | + def security_group_create( |
| 70 | + self, |
| 71 | + name=None, |
| 72 | + description=None, |
| 73 | + ): |
| 74 | + """Create a new security group |
| 75 | +
|
| 76 | + https://developer.openstack.org/api-ref/compute/#create-security-group |
| 77 | +
|
| 78 | + :param string name: |
| 79 | + Security group name |
| 80 | + :param integer description: |
| 81 | + Security group description |
| 82 | + """ |
| 83 | + |
| 84 | + url = "/os-security-groups" |
| 85 | + |
| 86 | + params = { |
| 87 | + 'name': name, |
| 88 | + 'description': description, |
| 89 | + } |
| 90 | + |
| 91 | + return self.create( |
| 92 | + url, |
| 93 | + json={'security_group': params}, |
| 94 | + )['security_group'] |
| 95 | + |
| 96 | + def security_group_delete( |
| 97 | + self, |
| 98 | + security_group=None, |
| 99 | + ): |
| 100 | + """Delete a security group |
| 101 | +
|
| 102 | + https://developer.openstack.org/api-ref/compute/#delete-security-group |
| 103 | +
|
| 104 | + :param string security_group: |
| 105 | + Security group name or ID |
| 106 | + """ |
| 107 | + |
| 108 | + url = "/os-security-groups" |
| 109 | + |
| 110 | + security_group = self.find( |
| 111 | + url, |
| 112 | + attr='name', |
| 113 | + value=security_group, |
| 114 | + )['id'] |
| 115 | + if security_group is not None: |
| 116 | + return self.delete('/%s/%s' % (url, security_group)) |
| 117 | + |
| 118 | + return None |
| 119 | + |
| 120 | + def security_group_find( |
| 121 | + self, |
| 122 | + security_group=None, |
| 123 | + ): |
| 124 | + """Return a security group given name or ID |
| 125 | +
|
| 126 | + https://developer.openstack.org/api-ref/compute/#show-security-group-details |
| 127 | +
|
| 128 | + :param string security_group: |
| 129 | + Security group name or ID |
| 130 | + :returns: A dict of the security group attributes |
| 131 | + """ |
| 132 | + |
| 133 | + url = "/os-security-groups" |
| 134 | + |
| 135 | + return self.find( |
| 136 | + url, |
| 137 | + attr='name', |
| 138 | + value=security_group, |
| 139 | + ) |
| 140 | + |
| 141 | + def security_group_list( |
| 142 | + self, |
| 143 | + limit=None, |
| 144 | + marker=None, |
| 145 | + search_opts=None, |
| 146 | + ): |
| 147 | + """Get security groups |
| 148 | +
|
| 149 | + https://developer.openstack.org/api-ref/compute/#list-security-groups |
| 150 | +
|
| 151 | + :param integer limit: |
| 152 | + query return count limit |
| 153 | + :param string marker: |
| 154 | + query marker |
| 155 | + :param search_opts: |
| 156 | + (undocumented) Search filter dict |
| 157 | + all_tenants: True|False - return all projects |
| 158 | + :returns: |
| 159 | + list of security groups names |
| 160 | + """ |
| 161 | + |
| 162 | + params = {} |
| 163 | + if search_opts is not None: |
| 164 | + params = dict((k, v) for (k, v) in search_opts.items() if v) |
| 165 | + if limit: |
| 166 | + params['limit'] = limit |
| 167 | + if marker: |
| 168 | + params['offset'] = marker |
| 169 | + |
| 170 | + url = "/os-security-groups" |
| 171 | + return self.list(url, **params)["security_groups"] |
| 172 | + |
| 173 | + def security_group_set( |
| 174 | + self, |
| 175 | + security_group=None, |
| 176 | + # name=None, |
| 177 | + # description=None, |
| 178 | + **params |
| 179 | + ): |
| 180 | + """Update a security group |
| 181 | +
|
| 182 | + https://developer.openstack.org/api-ref/compute/#update-security-group |
| 183 | +
|
| 184 | + :param string security_group: |
| 185 | + Security group name or ID |
| 186 | +
|
| 187 | + TODO(dtroyer): Create an update method in osc-lib |
| 188 | + """ |
| 189 | + |
| 190 | + # Short-circuit no-op |
| 191 | + if params is None: |
| 192 | + return None |
| 193 | + |
| 194 | + url = "/os-security-groups" |
| 195 | + |
| 196 | + security_group = self.find( |
| 197 | + url, |
| 198 | + attr='name', |
| 199 | + value=security_group, |
| 200 | + ) |
| 201 | + if security_group is not None: |
| 202 | + for (k, v) in params.items(): |
| 203 | + # Only set a value if it is already present |
| 204 | + if k in security_group: |
| 205 | + security_group[k] = v |
| 206 | + return self._request( |
| 207 | + "PUT", |
| 208 | + "/%s/%s" % (url, security_group['id']), |
| 209 | + json={'security_group': security_group}, |
| 210 | + ).json()['security_group'] |
| 211 | + return None |
0 commit comments