aboutsummaryrefslogtreecommitdiffstats
path: root/onap_data_provider/resources/esr_system_info_resource.py
blob: 4c26bbb60146322fee783fc238d83f4c0d005883 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""External system info 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 CloudRegion, EsrSystemInfo  # type: ignore
from onapsdk.exceptions import APIError  # type: ignore

from .resource import Resource


class EsrSystemInfoResource(Resource):
    """ESR system info resource class."""

    def __init__(self, data: Dict[str, Any], cloud_region: CloudRegion) -> None:
        """ESR system info resource initialization.

        Args:
            data (Dict[str, Any]): Data needed to create esr system info
            cloud_region (CloudRegion): Cloud region for which esr system info is going to be created

        """
        super().__init__(data)
        self.cloud_region: CloudRegion = cloud_region
        self._esr_system_info: EsrSystemInfo = None

    @staticmethod
    def get_esr_info_by_id(
        cloud_region: CloudRegion, esr_syste_info_id: str
    ) -> EsrSystemInfo:
        """Get esr system info from Cloud region by it's ID.

        Iterate through cloud region's esr system infos and check
            if it's already have some with provided ID.

        Args:
            cloud_region (CloudRegion): CloudRegion object to check if esr system info already exists
            esr_syste_info_id (str): ESR system info ID to check.

        Returns:
            EsrSystemInfo: ESR system info object
        """
        for esr_system_info in cloud_region.esr_system_infos:
            if esr_system_info.esr_system_info_id == esr_syste_info_id:
                return esr_system_info

    def create(self) -> None:
        """Create ESR system info resource.

        Add ESR system info to provided cloud region

        """
        logging.debug(
            "Create ESR system info for %s cloud region",
            self.cloud_region.cloud_region_id,
        )
        if not self.exists:
            self.cloud_region.add_esr_system_info(
                esr_system_info_id=self.data["esr-system-info-id"],
                user_name=self.data["user-name"],
                password=self.data["password"],
                system_type=self.data["system-type"],
                service_url=self.data["service-url"],
                system_status="active",
                cloud_domain=self.data["cloud-domain"],
                default_tenant=self.data.get("default-tenant"),
            )
            self._esr_system_info = self.get_esr_info_by_id(
                self.cloud_region, self.data["esr-system-info-id"]
            )

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

        Returns:
            bool: True if object exists, False otherwise

        """
        return self.esr_system_info is not None

    @property
    def esr_system_info(self) -> EsrSystemInfo:
        """External system info property.

        Returns:
            EsrSystemInfo: EsrSystemInfo object

        """
        if self._esr_system_info is None:
            try:
                if (
                    esr_system_info := self.get_esr_info_by_id(
                        self.cloud_region, self.data["esr-system-info-id"]
                    )
                ) is not None:
                    self._esr_system_info = esr_system_info
            except APIError:
                logging.info("No esr system infos")
        return self._esr_system_info