Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright 2017 Mirantis, Inc. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain 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, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | ''' |
| 16 | Management of Contrail resources |
| 17 | ================================ |
| 18 | |
| 19 | :depends: - vnc_api Python module |
| 20 | |
| 21 | |
| 22 | Enforce the virtual router existence |
| 23 | ------------------------------------ |
| 24 | |
| 25 | .. code-block:: yaml |
| 26 | |
| 27 | virtual_router: |
| 28 | contrail.virtual_router_present: |
| 29 | name: cmp01 |
| 30 | ip_address: 10.0.0.23 |
| 31 | dpdk_enabled: False |
| 32 | |
| 33 | |
| 34 | Enforce the virtual router absence |
| 35 | ---------------------------------- |
| 36 | |
| 37 | .. code-block:: yaml |
| 38 | |
| 39 | virtual_router_cmp01: |
| 40 | contrail.virtual_router_absent: |
| 41 | name: cmp01 |
| 42 | |
| 43 | |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 44 | Enforce the link local service entry existence |
| 45 | ---------------------------------------------- |
| 46 | |
| 47 | .. code-block:: yaml |
| 48 | |
| 49 | # Example with dns name, only one is permited |
| 50 | lls_meta1: |
| 51 | contrail.linklocal_service_present: |
| 52 | - name: meta1 |
| 53 | - lls_ip: 10.0.0.23 |
| 54 | - lls_port: 80 |
| 55 | - ipf_addresses: "meta.example.com" |
| 56 | - ipf_port: 80 |
| 57 | |
| 58 | # Example with multiple ip addresses |
| 59 | lls_meta2: |
| 60 | contrail.linklocal_service_present: |
| 61 | - name: meta2 |
| 62 | - lls_ip: 10.0.0.23 |
| 63 | - lls_port: 80 |
| 64 | - ipf_addresses: |
| 65 | - 10.10.10.10 |
| 66 | - 10.20.20.20 |
| 67 | - 10.30.30.30 |
| 68 | - ipf_port: 80 |
| 69 | |
| 70 | # Example with one ip addresses |
| 71 | lls_meta3: |
| 72 | contrail.linklocal_service_present: |
| 73 | - name: meta3 |
| 74 | - lls_ip: 10.0.0.23 |
| 75 | - lls_port: 80 |
| 76 | - ipf_addresses: |
| 77 | - 10.10.10.10 |
| 78 | - ipf_port: 80 |
| 79 | |
| 80 | |
| 81 | Enforce the link local service entry absence |
| 82 | -------------------------------------------- |
| 83 | |
| 84 | .. code-block:: yaml |
| 85 | |
| 86 | lls_meta1_delete: |
| 87 | contrail.linklocal_service_absent: |
| 88 | - name: cmp01 |
| 89 | |
| 90 | |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 91 | Enforce the analytics node existence |
| 92 | ------------------------------------ |
| 93 | |
| 94 | .. code-block:: yaml |
| 95 | |
| 96 | analytics_node01: |
| 97 | contrail.analytics_node_present: |
| 98 | name: nal01 |
| 99 | ip_address: 10.0.0.13 |
| 100 | |
| 101 | |
| 102 | Enforce the config node existence |
| 103 | --------------------------------- |
| 104 | |
| 105 | .. code-block:: yaml |
| 106 | |
| 107 | config_node01: |
| 108 | contrail.config_node_present: |
| 109 | name: ntw01 |
| 110 | ip_address: 10.0.0.23 |
| 111 | |
| 112 | |
| 113 | Enforce the database node existence |
| 114 | ----------------------------------- |
| 115 | |
| 116 | .. code-block:: yaml |
| 117 | |
| 118 | config_node01: |
| 119 | contrail.database_node_present: |
| 120 | name: ntw01 |
| 121 | ip_address: 10.0.0.33 |
| 122 | |
| 123 | ''' |
| 124 | |
| 125 | def __virtual__(): |
| 126 | ''' |
| 127 | Load Contrail module |
| 128 | ''' |
| 129 | return 'contrail' |
| 130 | |
| 131 | |
| 132 | def virtual_router_present(name, ip_address, dpdk_enabled=False, **kwargs): |
| 133 | ''' |
| 134 | Ensures that the Contrail virtual router exists. |
| 135 | |
| 136 | :param name: Virtual router name |
| 137 | :param ip_address: Virtual router IP address |
| 138 | ''' |
| 139 | ret = {'name': name, |
| 140 | 'changes': {}, |
| 141 | 'result': True, |
| 142 | 'comment': 'Virtual router "{0}" already exists'.format(name)} |
| 143 | virtual_router = __salt__['contrail.virtual_router_get'](name, **kwargs) |
| 144 | if 'Error' not in virtual_router: |
| 145 | pass |
| 146 | else: |
| 147 | __salt__['contrail.virtual_router_create'](name, ip_address, dpdk_enabled, **kwargs) |
| 148 | ret['comment'] = 'Virtual router {0} has been created'.format(name) |
| 149 | ret['changes']['VirtualRouter'] = 'Created' |
| 150 | return ret |
| 151 | |
| 152 | |
| 153 | def virtual_router_absent(name, **kwargs): |
| 154 | ''' |
| 155 | Ensure that the Contrail virtual router doesn't exist |
| 156 | |
| 157 | :param name: The name of the virtual router that should not exist |
| 158 | ''' |
| 159 | ret = {'name': name, |
| 160 | 'changes': {}, |
| 161 | 'result': True, |
| 162 | 'comment': 'Virtual router "{0}" is already absent'.format(name)} |
| 163 | virtual_router = __salt__['contrail.virtual_router_get'](name, **kwargs) |
| 164 | if 'Error' not in virtual_router: |
| 165 | __salt__['contrail.virtual_router_delete'](name, **kwargs) |
| 166 | ret['comment'] = 'Virtual router {0} has been deleted'.format(name) |
| 167 | ret['changes']['VirtualRouter'] = 'Deleted' |
| 168 | |
| 169 | return ret |
| 170 | |
| 171 | |
Petr Jediný | 5f3efe3 | 2017-05-26 17:55:09 +0200 | [diff] [blame] | 172 | def linklocal_service_present(name, lls_ip, lls_port, ipf_addresses, ipf_port, **kwargs): |
| 173 | ''' |
| 174 | Ensures that the Contrail link local service entry exists. |
| 175 | |
| 176 | :param name: Link local service name |
| 177 | :param lls_ip: Link local ip address |
| 178 | :param lls_port: Link local service port |
| 179 | :param ipf_addresses: IP fabric dns name or list of IP fabric ip addresses |
| 180 | :param ipf_port: IP fabric port |
| 181 | ''' |
| 182 | ret = {'name': name, |
| 183 | 'changes': {}, |
| 184 | 'result': True, |
| 185 | 'comment': 'Link local service "{0}" already exists'.format(name)} |
| 186 | lls = __salt__['contrail.linklocal_service_get'](name, **kwargs) |
| 187 | if 'Error' in lls: |
| 188 | __salt__['contrail.linklocal_service_create'](name, lls_ip, lls_port, ipf_addresses, ipf_port, **kwargs) |
| 189 | ret['comment'] = 'Link local service "{0}" has been created'.format(name) |
| 190 | ret['changes']['LinkLocalService'] = 'Created' |
| 191 | return ret |
| 192 | |
| 193 | |
| 194 | def linklocal_service_absent(name, **kwargs): |
| 195 | ''' |
| 196 | Ensure that the Contrail link local service entry doesn't exist |
| 197 | |
| 198 | :param name: The name of the link local service entry |
| 199 | ''' |
| 200 | ret = {'name': name, |
| 201 | 'changes': {}, |
| 202 | 'result': True, |
| 203 | 'comment': ' "{0}" is already absent'.format(name)} |
| 204 | lls = __salt__['contrail.linklocal_service_get'](name, **kwargs) |
| 205 | if 'Error' not in lls: |
| 206 | __salt__['contrail.linklocal_service_delete'](name, **kwargs) |
| 207 | ret['comment'] = 'Link local service "{0}" has been deleted'.format(name) |
| 208 | ret['changes']['LinkLocalService'] = 'Deleted' |
| 209 | |
| 210 | return ret |
| 211 | |
Ales Komarek | ad46d2e | 2017-03-09 17:16:38 +0100 | [diff] [blame] | 212 | def analytics_node_present(name, ip_address, **kwargs): |
| 213 | ''' |
| 214 | Ensures that the Contrail analytics node exists. |
| 215 | |
| 216 | :param name: Analytics node name |
| 217 | ''' |
| 218 | ret = {'name': name, |
| 219 | 'changes': {}, |
| 220 | 'result': True, |
| 221 | 'comment': 'Analytics node {0} already exists'.format(name)} |
| 222 | analytics_node = __salt__['contrail.analytics_node_get'](name, **kwargs) |
| 223 | if 'Error' not in analytics_node: |
| 224 | pass |
| 225 | else: |
| 226 | __salt__['contrail.analytics_node_create'](name, ip_address, **kwargs) |
| 227 | ret['comment'] = 'Analytics node {0} has been created'.format(name) |
| 228 | ret['changes']['AnalyticsNode'] = 'Created' |
| 229 | return ret |
| 230 | |
| 231 | |
| 232 | def config_node_present(name, ip_address, **kwargs): |
| 233 | ''' |
| 234 | Ensures that the Contrail config node exists. |
| 235 | |
| 236 | :param name: Config node name |
| 237 | ''' |
| 238 | ret = {'name': name, |
| 239 | 'changes': {}, |
| 240 | 'result': True, |
| 241 | 'comment': 'Config node {0} already exists'.format(name)} |
| 242 | config_node = __salt__['contrail.config_node_get'](name, **kwargs) |
| 243 | if 'Error' not in config_node: |
| 244 | pass |
| 245 | else: |
| 246 | __salt__['contrail.config_node_create'](name, ip_address, **kwargs) |
| 247 | ret['comment'] = 'Config node {0} has been created'.format(name) |
| 248 | ret['changes']['ConfigNode'] = 'Created' |
| 249 | return ret |
| 250 | |
| 251 | |
| 252 | def bgp_router_present(name, type, ip_address, asn=64512, **kwargs): |
| 253 | ''' |
| 254 | Ensures that the Contrail BGP router exists. |
| 255 | |
| 256 | :param name: BGP router name |
| 257 | ''' |
| 258 | ret = {'name': name, |
| 259 | 'changes': {}, |
| 260 | 'result': True, |
| 261 | 'comment': 'BGP router {0} already exists'.format(name)} |
| 262 | bgp_router = __salt__['contrail.bgp_router_get'](name, **kwargs) |
| 263 | if 'Error' not in bgp_router: |
| 264 | pass |
| 265 | else: |
| 266 | __salt__['contrail.bgp_router_create'](name, type, ip_address, asn, **kwargs) |
| 267 | ret['comment'] = 'BGP router {0} has been created'.format(name) |
| 268 | ret['changes']['BgpRouter'] = 'Created' |
| 269 | return ret |
| 270 | |
| 271 | |
| 272 | def database_node_present(name, ip_address, **kwargs): |
| 273 | ''' |
| 274 | Ensures that the Contrail database node exists. |
| 275 | |
| 276 | :param name: Database node name |
| 277 | ''' |
| 278 | ret = {'name': name, |
| 279 | 'changes': {}, |
| 280 | 'result': True, |
| 281 | 'comment': 'Database node {0} already exists'.format(name)} |
| 282 | database_node = __salt__['contrail.database_node_get'](name, **kwargs) |
| 283 | if 'Error' not in database_node: |
| 284 | pass |
| 285 | else: |
| 286 | __salt__['contrail.database_node_create'](name, ip_address, **kwargs) |
| 287 | ret['comment'] = 'Database node {0} has been created'.format(name) |
| 288 | ret['changes']['DatabaseNode'] = 'Created' |
| 289 | return ret |