aboutsummaryrefslogtreecommitdiffstats
path: root/src/onaptests/steps/cloud/register_cloud.py
blob: 30654614ddf881f6e9e8a776c365e44d9ab62711 (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
"""A&AI cloud region registstation module."""
import time
from uuid import uuid4

from onapsdk.aai.cloud_infrastructure import CloudRegion
from onapsdk.configuration import settings
from onapsdk.exceptions import ResourceNotFound

from ..base import BaseStep
from onaptests.steps.cloud.cloud_region_create import CloudRegionCreateStep


class RegisterCloudRegionStep(BaseStep):
    """Cloud region registration step."""

    def __init__(self, cleanup: bool) -> None:
        """Initialize step.

        Substeps:
            - CloudRegionCreateStep.
        """
        super().__init__(cleanup=cleanup)
        self.add_step(CloudRegionCreateStep(cleanup=cleanup))

    @property
    def description(self) -> str:
        """Step description."""
        return "Register cloud region."

    @property
    def component(self) -> str:
        """Component name."""
        return "AAI"

    @BaseStep.store_state
    def execute(self):
        """Register cloud region.

        Use settings values:
         - CLOUD_REGION_CLOUD_OWNER,
         - CLOUD_REGION_ID,
         - CLOUD_DOMAIN,
         - VIM_USERNAME,
         - VIM_PASSWORD,
         - VIM_SERVICE_URL,
         - TENANT_NAME.
        """
        super().execute()
        cloud_region: CloudRegion = CloudRegion.get_by_id(
            cloud_owner=settings.CLOUD_REGION_CLOUD_OWNER,
            cloud_region_id=settings.CLOUD_REGION_ID)
        cloud_region.add_esr_system_info(
            esr_system_info_id=str(uuid4()),
            user_name=settings.VIM_USERNAME,
            password=settings.VIM_PASSWORD,
            system_type="VIM",
            service_url=settings.VIM_SERVICE_URL,
            ssl_insecure=False,
            system_status="active",
            cloud_domain=settings.CLOUD_DOMAIN,
            default_tenant=settings.TENANT_NAME
        )
        if settings.USE_MULTICLOUD:
            self._logger.info("*Multicloud registration *")
            cloud_region.register_to_multicloud()
            time.sleep(20)
            nb_try = 0
            nb_try_max = 3
            while nb_try < nb_try_max:
                try:
                    if not cloud_region.tenants:
                        self._logger.debug("No tenats available, check one more time")
                        time.sleep(20)
                    else:
                        break
                except ResourceNotFound:
                    self._logger.debug("No tenats available, check one more time")
                    time.sleep(20)
                nb_try += 1

        # Retrieve the tenant, created by multicloud registration
        # if it does not exist, create it
        try:
            cloud_region.get_tenant(settings.TENANT_ID)
        except ResourceNotFound:
            self._logger.warning("Impossible to retrieve the Specificed Tenant")
            self._logger.debug("If no multicloud selected, add the tenant, reraise otherwise")
            if not settings.USE_MULTICLOUD:
                cloud_region.add_tenant(
                    tenant_id=settings.TENANT_ID,
                    tenant_name=settings.TENANT_NAME)
            else:
                raise

        # be sure that an availability zone has been created
        # if not, create it
        try:
            cloud_region.get_availability_zone_by_name(
                settings.AVAILABILITY_ZONE_NAME)
        except ResourceNotFound:
            cloud_region.add_availability_zone(
                settings.AVAILABILITY_ZONE_NAME,
                settings.AVAILABILITY_ZONE_TYPE)