Deb package creation for NG-SA
[osm/NG-SA.git] / src / osm_ngsa / osm_mon / vim_connectors / gcp.py
1 #######################################################################################
2 # Copyright ETSI Contributors and Others.
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
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #######################################################################################
17 # pylint: disable=E1101
18
19 import logging
20 from typing import Dict, List
21
22 from google.oauth2 import service_account
23 import googleapiclient.discovery
24 from osm_mon.vim_connectors.base_vim import VIMConnector
25
26 log = logging.getLogger(__name__)
27
28
29 class GcpCollector(VIMConnector):
30 def __init__(self, vim_account: Dict):
31 self.vim_account = vim_account
32 self.project = vim_account["vim_tenant_name"] or vim_account["vim_tenant_id"]
33
34 # REGION - Google Cloud considers regions and zones. A specific region
35 # can have more than one zone (for instance: region us-west1 with the
36 # zones us-west1-a, us-west1-b and us-west1-c). So the region name
37 # specified in the config will be considered as a specific zone for GC
38 # and the region will be calculated from that without the preffix.
39 if "config" in vim_account:
40 config = vim_account["config"]
41 if "region_name" in config:
42 self.zone = config.get("region_name")
43 self.region = self.zone.rsplit("-", 1)[0]
44 else:
45 log.error("Google Cloud region_name not specified in config")
46 else:
47 log.error("config is not specified in VIM")
48
49 # Credentials
50 scopes = ["https://www.googleapis.com/auth/cloud-platform"]
51 self.credentials = None
52 if "credentials" in config:
53 log.debug("Setting credentials")
54 # Settings Google Cloud credentials dict
55 creds_body = config["credentials"]
56 creds = service_account.Credentials.from_service_account_info(creds_body)
57 if "sa_file" in config:
58 creds = service_account.Credentials.from_service_account_file(
59 config.get("sa_file"), scopes=scopes
60 )
61 log.debug("Credentials: %s", creds)
62 # Construct a Resource for interacting with an API.
63 self.credentials = creds
64 try:
65 self.conn_compute = googleapiclient.discovery.build(
66 "compute", "v1", credentials=creds
67 )
68 except Exception as e:
69 log.error(e)
70 else:
71 log.error("It is not possible to init GCP with no credentials")
72
73 def collect_servers_status(self) -> List[Dict]:
74 servers = []
75 try:
76 response = (
77 self.conn_compute.instances()
78 .list(project=self.project, zone=self.zone)
79 .execute()
80 )
81 if "items" in response:
82 log.info(response["items"])
83 for server in response["items"]:
84 vm = {
85 "id": server["id"],
86 "name": server["name"],
87 "status": (1 if (server["status"] == "RUNNING") else 0),
88 }
89 servers.append(vm)
90 except Exception as e:
91 log.error(e)
92 return servers
93
94 def is_vim_ok(self) -> bool:
95 status = False
96 try:
97 self.conn_compute.zones().get(
98 project=self.project, zone=self.zone
99 ).execute()
100 status = True
101 except Exception as e:
102 log.error(e)
103 return status