aboutsummaryrefslogtreecommitdiffstats
path: root/onap_data_provider/resources/complex_resource.py
blob: 82ab4625bcbb2b70265d3a9f78fbad41a317566c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""Complex resource module."""
"""
   Copyright 2021 Deutsche Telekom AG

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""

import logging
from typing import Any, Dict

from onapsdk.aai.cloud_infrastructure import Complex  # type: ignore

from .resource import Resource
from onapsdk.exceptions import ResourceNotFound  # type: ignore


class ComplexResource(Resource):
    """Complex resource class."""

    def __init__(self, data: Dict[str, Any]) -> None:
        """Complex resource initialization.

        Args:
            data (Dict[str, Any]): Data needed to create complex

        """
        super().__init__(data)
        self._complex: Complex = None

    def create(self) -> None:
        """Create complex resource."""
        if not self.exists:
            self._complex = Complex.create(
                physical_location_id=self.data["physical-location-id"],
                name=self.data.get("complex-name"),
                data_center_code=self.data.get("data-center-code"),
                identity_url=self.data.get("identity-url"),
                physical_location_type=self.data.get("physical-location-type"),
                street1=self.data.get("street1"),
                street2=self.data.get("street2"),
                city=self.data.get("city"),
                state=self.data.get("state"),
                postal_code=self.data.get("postal-code"),
                country=self.data.get("country"),
                region=self.data.get("region"),
                latitude=self.data.get("latitude"),
                longitude=self.data.get("longitude"),
                elevation=self.data.get("elevation"),
                lata=self.data.get("lata"),
            )

    @property
    def exists(self) -> bool:
        """Determine if resource already exists or not.

        Returns:
            bool: True if object exists, False otherwise

        """
        return self.complex is not None

    @property
    def complex(self) -> Complex:
        """Complex property.

        Returns:
            Complex: Complex object

        """
        if not self._complex:
            try:
                self._complex = next(
                    Complex.get_all(
                        physical_location_id=self.data["physical-location-id"]
                    )
                )
            except ResourceNotFound:
                logging.error(
                    "Complex %s does not exist", self.data["physical-location-id"]
                )
                return None
        return self._complex