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 | |
| 44 | Enforce the analytics node existence |
| 45 | ------------------------------------ |
| 46 | |
| 47 | .. code-block:: yaml |
| 48 | |
| 49 | analytics_node01: |
| 50 | contrail.analytics_node_present: |
| 51 | name: nal01 |
| 52 | ip_address: 10.0.0.13 |
| 53 | |
| 54 | |
| 55 | Enforce the config node existence |
| 56 | --------------------------------- |
| 57 | |
| 58 | .. code-block:: yaml |
| 59 | |
| 60 | config_node01: |
| 61 | contrail.config_node_present: |
| 62 | name: ntw01 |
| 63 | ip_address: 10.0.0.23 |
| 64 | |
| 65 | |
| 66 | Enforce the database node existence |
| 67 | ----------------------------------- |
| 68 | |
| 69 | .. code-block:: yaml |
| 70 | |
| 71 | config_node01: |
| 72 | contrail.database_node_present: |
| 73 | name: ntw01 |
| 74 | ip_address: 10.0.0.33 |
| 75 | |
| 76 | ''' |
| 77 | |
| 78 | def __virtual__(): |
| 79 | ''' |
| 80 | Load Contrail module |
| 81 | ''' |
| 82 | return 'contrail' |
| 83 | |
| 84 | |
| 85 | def virtual_router_present(name, ip_address, dpdk_enabled=False, **kwargs): |
| 86 | ''' |
| 87 | Ensures that the Contrail virtual router exists. |
| 88 | |
| 89 | :param name: Virtual router name |
| 90 | :param ip_address: Virtual router IP address |
| 91 | ''' |
| 92 | ret = {'name': name, |
| 93 | 'changes': {}, |
| 94 | 'result': True, |
| 95 | 'comment': 'Virtual router "{0}" already exists'.format(name)} |
| 96 | virtual_router = __salt__['contrail.virtual_router_get'](name, **kwargs) |
| 97 | if 'Error' not in virtual_router: |
| 98 | pass |
| 99 | else: |
| 100 | __salt__['contrail.virtual_router_create'](name, ip_address, dpdk_enabled, **kwargs) |
| 101 | ret['comment'] = 'Virtual router {0} has been created'.format(name) |
| 102 | ret['changes']['VirtualRouter'] = 'Created' |
| 103 | return ret |
| 104 | |
| 105 | |
| 106 | def virtual_router_absent(name, **kwargs): |
| 107 | ''' |
| 108 | Ensure that the Contrail virtual router doesn't exist |
| 109 | |
| 110 | :param name: The name of the virtual router that should not exist |
| 111 | ''' |
| 112 | ret = {'name': name, |
| 113 | 'changes': {}, |
| 114 | 'result': True, |
| 115 | 'comment': 'Virtual router "{0}" is already absent'.format(name)} |
| 116 | virtual_router = __salt__['contrail.virtual_router_get'](name, **kwargs) |
| 117 | if 'Error' not in virtual_router: |
| 118 | __salt__['contrail.virtual_router_delete'](name, **kwargs) |
| 119 | ret['comment'] = 'Virtual router {0} has been deleted'.format(name) |
| 120 | ret['changes']['VirtualRouter'] = 'Deleted' |
| 121 | |
| 122 | return ret |
| 123 | |
| 124 | |
| 125 | def analytics_node_present(name, ip_address, **kwargs): |
| 126 | ''' |
| 127 | Ensures that the Contrail analytics node exists. |
| 128 | |
| 129 | :param name: Analytics node name |
| 130 | ''' |
| 131 | ret = {'name': name, |
| 132 | 'changes': {}, |
| 133 | 'result': True, |
| 134 | 'comment': 'Analytics node {0} already exists'.format(name)} |
| 135 | analytics_node = __salt__['contrail.analytics_node_get'](name, **kwargs) |
| 136 | if 'Error' not in analytics_node: |
| 137 | pass |
| 138 | else: |
| 139 | __salt__['contrail.analytics_node_create'](name, ip_address, **kwargs) |
| 140 | ret['comment'] = 'Analytics node {0} has been created'.format(name) |
| 141 | ret['changes']['AnalyticsNode'] = 'Created' |
| 142 | return ret |
| 143 | |
| 144 | |
| 145 | def config_node_present(name, ip_address, **kwargs): |
| 146 | ''' |
| 147 | Ensures that the Contrail config node exists. |
| 148 | |
| 149 | :param name: Config node name |
| 150 | ''' |
| 151 | ret = {'name': name, |
| 152 | 'changes': {}, |
| 153 | 'result': True, |
| 154 | 'comment': 'Config node {0} already exists'.format(name)} |
| 155 | config_node = __salt__['contrail.config_node_get'](name, **kwargs) |
| 156 | if 'Error' not in config_node: |
| 157 | pass |
| 158 | else: |
| 159 | __salt__['contrail.config_node_create'](name, ip_address, **kwargs) |
| 160 | ret['comment'] = 'Config node {0} has been created'.format(name) |
| 161 | ret['changes']['ConfigNode'] = 'Created' |
| 162 | return ret |
| 163 | |
| 164 | |
| 165 | def bgp_router_present(name, type, ip_address, asn=64512, **kwargs): |
| 166 | ''' |
| 167 | Ensures that the Contrail BGP router exists. |
| 168 | |
| 169 | :param name: BGP router name |
| 170 | ''' |
| 171 | ret = {'name': name, |
| 172 | 'changes': {}, |
| 173 | 'result': True, |
| 174 | 'comment': 'BGP router {0} already exists'.format(name)} |
| 175 | bgp_router = __salt__['contrail.bgp_router_get'](name, **kwargs) |
| 176 | if 'Error' not in bgp_router: |
| 177 | pass |
| 178 | else: |
| 179 | __salt__['contrail.bgp_router_create'](name, type, ip_address, asn, **kwargs) |
| 180 | ret['comment'] = 'BGP router {0} has been created'.format(name) |
| 181 | ret['changes']['BgpRouter'] = 'Created' |
| 182 | return ret |
| 183 | |
| 184 | |
| 185 | def database_node_present(name, ip_address, **kwargs): |
| 186 | ''' |
| 187 | Ensures that the Contrail database node exists. |
| 188 | |
| 189 | :param name: Database node name |
| 190 | ''' |
| 191 | ret = {'name': name, |
| 192 | 'changes': {}, |
| 193 | 'result': True, |
| 194 | 'comment': 'Database node {0} already exists'.format(name)} |
| 195 | database_node = __salt__['contrail.database_node_get'](name, **kwargs) |
| 196 | if 'Error' not in database_node: |
| 197 | pass |
| 198 | else: |
| 199 | __salt__['contrail.database_node_create'](name, ip_address, **kwargs) |
| 200 | ret['comment'] = 'Database node {0} has been created'.format(name) |
| 201 | ret['changes']['DatabaseNode'] = 'Created' |
| 202 | return ret |