1 #######################################################################################
2 # Copyright ETSI Contributors and Others.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #######################################################################################
17 # pylint: disable=E1101
20 from typing
import Dict
, List
22 from google
.oauth2
import service_account
23 import googleapiclient
.discovery
24 from osm_mon
.vim_connectors
.base_vim
import VIMConnector
26 log
= logging
.getLogger(__name__
)
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"]
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]
45 log
.error("Google Cloud region_name not specified in config")
47 log
.error("config is not specified in VIM")
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
61 log
.debug("Credentials: %s", creds
)
62 # Construct a Resource for interacting with an API.
63 self
.credentials
= creds
65 self
.conn_compute
= googleapiclient
.discovery
.build(
66 "compute", "v1", credentials
=creds
68 except Exception as e
:
71 log
.error("It is not possible to init GCP with no credentials")
73 def collect_servers_status(self
) -> List
[Dict
]:
77 self
.conn_compute
.instances()
78 .list(project
=self
.project
, zone
=self
.zone
)
81 if "items" in response
:
82 log
.info(response
["items"])
83 for server
in response
["items"]:
86 "name": server
["name"],
87 "status": (1 if (server
["status"] == "RUNNING") else 0),
90 except Exception as e
:
94 def is_vim_ok(self
) -> bool:
97 self
.conn_compute
.zones().get(
98 project
=self
.project
, zone
=self
.zone
101 except Exception as e
: