Merge "Remove Compass from genesis."
[genesis.git] / opensteak / tools / opensteak / foreman_objects / subnets.py
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14 #
15 # Authors:
16 # @author: David Blaisonneau <david.blaisonneau@orange.com>
17 # @author: Arnaud Morin <arnaud1.morin@orange.com>
18
19 from opensteak.foreman_objects.objects import ForemanObjects
20
21
22 class Subnets(ForemanObjects):
23     """
24     Subnets class
25     """
26     objName = 'subnets'
27     payloadObj = 'subnet'
28
29     def checkAndCreate(self, key, payload, domainId):
30         """ Function checkAndCreate
31         Check if a subnet exists and create it if not
32
33         @param key: The targeted subnet
34         @param payload: The targeted subnet description
35         @param domainId: The domainId to be attached wiuth the subnet
36         @return RETURN: The id of the subnet
37         """
38         if key not in self:
39             self[key] = payload
40         oid = self[key]['id']
41         if not oid:
42             return False
43         #~ Ensure subnet contains the domain
44         subnetDomainIds = []
45         for domain in self[key]['domains']:
46             subnetDomainIds.append(domain['id'])
47         if domainId not in subnetDomainIds:
48             subnetDomainIds.append(domainId)
49             self[key]["domain_ids"] = subnetDomainIds
50             if len(self[key]["domains"]) is not len(subnetDomainIds):
51                 return False
52         return oid
53
54     def removeDomain(self, subnetId, domainId):
55         """ Function removeDomain
56         Delete a domain from a subnet
57
58         @param subnetId: The subnet Id
59         @param domainId: The domainId to be attached wiuth the subnet
60         @return RETURN: boolean
61         """
62         subnetDomainIds = []
63         for domain in self[subnetId]['domains']:
64             subnetDomainIds.append(domain['id'])
65         subnetDomainIds.remove(domainId)
66         self[subnetId]["domain_ids"] = subnetDomainIds
67         return len(self[subnetId]["domains"]) is len(subnetDomainIds)