gcpdiag.queries.cloudsql

Queries related to CloudSQL.
class Instance(gcpdiag.models.Resource):
 26class Instance(models.Resource):
 27  """ Represents CloudSQL Instance"""
 28  _resource_data: dict
 29
 30  def __init__(self, project_id: str, resource_data: dict):
 31    super().__init__(project_id)
 32    self._resource_data = resource_data
 33
 34  @property
 35  def name(self) -> str:
 36    return self._resource_data['name']
 37
 38  @property
 39  def state(self) -> str:
 40    return self._resource_data['state']
 41
 42  @property
 43  def version(self) -> str:
 44    return self._resource_data['databaseVersion']
 45
 46  @property
 47  def is_regional(self) -> bool:
 48    return get_path(self._resource_data, ('settings', 'availabilityType'),
 49                    default='ZONAL') == 'REGIONAL'
 50
 51  @property
 52  def ip_addresses(self) -> Iterable[network.IPv4AddrOrIPv6Addr]:
 53    return [
 54        ipaddress.ip_address(nic['ipAddress'])
 55        for nic in self._resource_data.get('ipAddresses', [])
 56    ]
 57
 58  @property
 59  def has_public_ip(self) -> bool:
 60    return get_path(self._resource_data,
 61                    ('settings', 'ipConfiguration', 'ipv4Enabled'))
 62
 63  @property
 64  def has_maint_window(self) -> int:
 65    try:
 66      return get_path(self._resource_data,
 67                      ('settings', 'maintenanceWindow', 'day'))
 68    except KeyError:
 69      return 0
 70
 71  @property
 72  def is_storage_auto_resize_enabled(self) -> bool:
 73    return get_path(self._resource_data, ('settings', 'storageAutoResize'))
 74
 75  @property
 76  def has_del_protection(self) -> bool:
 77    return get_path(self._resource_data,
 78                    ('settings', 'deletionProtectionEnabled'), False)
 79
 80  @property
 81  def authorizednetworks(self) -> List[str]:
 82    authorizednetworks = get_path(
 83        self._resource_data,
 84        ('settings', 'ipConfiguration', 'authorizedNetworks'), [])
 85    return [
 86        authorizednetwork['value'] for authorizednetwork in authorizednetworks
 87    ]
 88
 89  @property
 90  def is_publically_accessible(self) -> List[str]:
 91    return self.authorizednetworks
 92
 93  @property
 94  def is_automated_backup_enabled(self) -> bool:
 95    return get_path(self._resource_data,
 96                    ('settings', 'backupConfiguration', 'enabled'))
 97
 98  @property
 99  def is_suspended_state(self) -> bool:
100    return self.state == 'SUSPENDED'
101
102  @property
103  def is_shared_core(self) -> bool:
104    shared_core_tiers = ['db-g1-small', 'db-f1-micro']
105    return get_path(self._resource_data,
106                    ('settings', 'tier')) in shared_core_tiers
107
108  @property
109  def is_high_available(self) -> bool:
110    return get_path(self._resource_data,
111                    ('settings', 'availabilityType')) == 'REGIONAL'
112
113  @property
114  def flags(self) -> dict:
115    flags = get_path(self._resource_data, ('settings', 'databaseFlags'), [])
116    return {flag['name']: flag['value'] for flag in flags}
117
118  @property
119  def is_log_output_configured_as_table(self) -> bool:
120    return self.flags.get('log_output') == 'TABLE'
121
122  @property
123  def self_link(self) -> str:
124    return self._resource_data['selfLink']
125
126  @property
127  def full_path(self) -> str:
128    return self.self_link
129
130  def __str__(self) -> str:
131    return self.self_link

Represents CloudSQL Instance

Instance(project_id: str, resource_data: dict)
30  def __init__(self, project_id: str, resource_data: dict):
31    super().__init__(project_id)
32    self._resource_data = resource_data
name: str
34  @property
35  def name(self) -> str:
36    return self._resource_data['name']
state: str
38  @property
39  def state(self) -> str:
40    return self._resource_data['state']
version: str
42  @property
43  def version(self) -> str:
44    return self._resource_data['databaseVersion']
is_regional: bool
46  @property
47  def is_regional(self) -> bool:
48    return get_path(self._resource_data, ('settings', 'availabilityType'),
49                    default='ZONAL') == 'REGIONAL'
ip_addresses: Iterable[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]]
51  @property
52  def ip_addresses(self) -> Iterable[network.IPv4AddrOrIPv6Addr]:
53    return [
54        ipaddress.ip_address(nic['ipAddress'])
55        for nic in self._resource_data.get('ipAddresses', [])
56    ]
has_public_ip: bool
58  @property
59  def has_public_ip(self) -> bool:
60    return get_path(self._resource_data,
61                    ('settings', 'ipConfiguration', 'ipv4Enabled'))
has_maint_window: int
63  @property
64  def has_maint_window(self) -> int:
65    try:
66      return get_path(self._resource_data,
67                      ('settings', 'maintenanceWindow', 'day'))
68    except KeyError:
69      return 0
is_storage_auto_resize_enabled: bool
71  @property
72  def is_storage_auto_resize_enabled(self) -> bool:
73    return get_path(self._resource_data, ('settings', 'storageAutoResize'))
has_del_protection: bool
75  @property
76  def has_del_protection(self) -> bool:
77    return get_path(self._resource_data,
78                    ('settings', 'deletionProtectionEnabled'), False)
authorizednetworks: List[str]
80  @property
81  def authorizednetworks(self) -> List[str]:
82    authorizednetworks = get_path(
83        self._resource_data,
84        ('settings', 'ipConfiguration', 'authorizedNetworks'), [])
85    return [
86        authorizednetwork['value'] for authorizednetwork in authorizednetworks
87    ]
is_publically_accessible: List[str]
89  @property
90  def is_publically_accessible(self) -> List[str]:
91    return self.authorizednetworks
is_automated_backup_enabled: bool
93  @property
94  def is_automated_backup_enabled(self) -> bool:
95    return get_path(self._resource_data,
96                    ('settings', 'backupConfiguration', 'enabled'))
is_suspended_state: bool
 98  @property
 99  def is_suspended_state(self) -> bool:
100    return self.state == 'SUSPENDED'
is_shared_core: bool
102  @property
103  def is_shared_core(self) -> bool:
104    shared_core_tiers = ['db-g1-small', 'db-f1-micro']
105    return get_path(self._resource_data,
106                    ('settings', 'tier')) in shared_core_tiers
is_high_available: bool
108  @property
109  def is_high_available(self) -> bool:
110    return get_path(self._resource_data,
111                    ('settings', 'availabilityType')) == 'REGIONAL'
flags: dict
113  @property
114  def flags(self) -> dict:
115    flags = get_path(self._resource_data, ('settings', 'databaseFlags'), [])
116    return {flag['name']: flag['value'] for flag in flags}
is_log_output_configured_as_table: bool
118  @property
119  def is_log_output_configured_as_table(self) -> bool:
120    return self.flags.get('log_output') == 'TABLE'
full_path: str
126  @property
127  def full_path(self) -> str:
128    return self.self_link

Returns the full path of this resource.

Example: 'projects/gcpdiag-gke-1-9b90/zones/europe-west4-a/clusters/gke1'

@caching.cached_api_call
def get_instances( context: gcpdiag.models.Context) -> Iterable[Instance]:
134@caching.cached_api_call
135def get_instances(context: models.Context) -> Iterable[Instance]:
136  if not apis.is_enabled(context.project_id, 'sqladmin'):
137    return []
138
139  api = apis.get_api('sqladmin', 'v1', context.project_id)
140  query = api.instances().list(project=context.project_id)
141  resp = query.execute(num_retries=config.API_RETRIES)
142  databases = []
143  for d in resp.get('items', []):
144    location = d.get('region', '')
145    labels = d.get('userLabels', {})
146    resource = d.get('name', '')
147    if not context.match_project_resource(
148        location=location, labels=labels, resource=resource):
149      continue
150
151    databases.append(Instance(context.project_id, d))
152  return databases