-
Notifications
You must be signed in to change notification settings - Fork 1
/
initialize.py
71 lines (52 loc) · 2.12 KB
/
initialize.py
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
#!/usr/bin/python3
"""
Copyright 2024 Google LLC
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.
Initialization of APIs.
"""
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
class ApiEnable:
def __init__(self):
self.creds = GoogleCredentials.get_application_default()
self.serviceusage = discovery.build("serviceusage", "v1", credentials=self.creds)
self.crm = discovery.build("cloudresourcemanager", "v1", credentials=self.creds)
self.iam = discovery.build("iam", "v1", credentials=self.creds)
def enable_api(self, project_id, api):
"""Enables the GCP service APIs
Args:
project_id : The ID of the project where the service status is being checked
"""
# Get the API service status in the project
api_status = self._get_service_api_status(
api, project_id
)
# Enable the API service in the project
if api_status != "ENABLED":
self.serviceusage.services().enable(
name=f"projects/{project_id}/services/{api}"
).execute()
print(f"Enabled {api} API in project : {project_id}")
def _get_service_api_status(self, api_name, project_id):
"""
Get the current status of a GCP service API
Args:
api_name : API name whose status is to be returned
project_id : The ID of the project where the service status is being checked
Returns:
string : service api status
"""
response = (
self.serviceusage.services()
.get(name=f"projects/{project_id}/services/{api_name}")
.execute()
)
return response["state"]