def expected_labels(self) -> set[ServiceLabel]: return { ServiceLabel("gcp/location", "us-central1-a"), ServiceLabel("gcp/filestore/name", "test"), ServiceLabel("gcp/projectId", "backup-255820"), ServiceLabel("gcp/labels/foo", "bar"), }
def expected_labels(self) -> set[ServiceLabel]: return { ServiceLabel("gcp/labels/tag", "freebackup"), ServiceLabel("gcp/location", "US-CENTRAL1"), ServiceLabel("gcp/bucket/storageClass", "STANDARD"), ServiceLabel("gcp/bucket/locationType", "region"), ServiceLabel("gcp/projectId", "backup-255820"), }
def discovery_migration_wrapper(section): disco_func = check_info_dict.get("inventory_function") if not callable(disco_func): # never discover: return original_discovery_result = disco_func(section) if not original_discovery_result: return for element in original_discovery_result: if isinstance(element, tuple) and len(element) in (2, 3): parameters = _resolve_string_parameters( element[-1], check_name, get_check_context) service = Service( item=element[0] or None, parameters=wrap_parameters(parameters or {}), ) # nasty hack for nasty plugins: # Bypass validation. Item should be None or non-empty string! service = service._replace(item=element[0]) yield service else: try: yield Service( item=element.item, parameters=wrap_parameters(element.parameters or {}), labels=[ ServiceLabel(l.name, l.value) for l in element.service_labels ], ) except AttributeError: # just let it through. Base must deal with bogus return types anyway. yield element
def test_discover_bucket_labels_without_user_labels(): asset_table = [ ['{"project":"backup-255820"}'], [ '{"name": "//storage.googleapis.com/backup-home-ml-free", "asset_type": "storage.googleapis.com/Bucket", "resource": {"version": "v1", "discovery_document_uri": "https://www.googleapis.com/discovery/v1/apis/storage/v1/rest", "discovery_name": "Bucket", "parent": "//cloudresourcemanager.googleapis.com/projects/360989076580", "data": {"name": "backup-home-ml-free", "id": "backup-home-ml-free", "labels": {}, "projectNumber": 360989076580.0, "timeCreated": "2019-11-03T13:48:57.905Z", "lifecycle": {"rule": []}, "metageneration": 1.0, "cors": [], "storageClass": "STANDARD", "etag": "CAE=", "kind": "storage#bucket", "billing": {}, "versioning": {}, "iamConfiguration": {"uniformBucketLevelAccess": {"enabled": false}, "bucketPolicyOnly": {"enabled": false}}, "owner": {}, "encryption": {}, "updated": "2019-11-03T13:48:57.905Z", "locationType": "region", "logging": {}, "acl": [], "retentionPolicy": {}, "defaultObjectAcl": [], "location": "US-CENTRAL1", "selfLink": "https://www.googleapis.com/storage/v1/b/backup-home-ml-free", "website": {}, "autoclass": {}}, "location": "us-central1", "resource_url": ""}, "ancestors": ["projects/360989076580"], "update_time": "2021-09-20T20:35:59.747Z", "org_policy": []}' ], ] asset_section = gcp.parse_assets(asset_table) buckets = list(discover(section_gcp_service_gcs=None, section_gcp_assets=asset_section)) labels = buckets[0].labels assert set(labels) == { ServiceLabel("gcp/location", "US-CENTRAL1"), ServiceLabel("gcp/bucket/storageClass", "STANDARD"), ServiceLabel("gcp/bucket/locationType", "region"), ServiceLabel("gcp/projectId", "backup-255820"), }
def test_discover_labels_labels_without_user_labels(): asset_table = [ ['{"project":"backup-255820"}'], [ '{"name": "//file.googleapis.com/projects/tribe29-check-development/locations/us-central1-a/instances/test", "asset_type": "file.googleapis.com/Instance", "resource": {"version": "v1", "discovery_document_uri": "https://file.googleapis.com/$discovery/rest", "discovery_name": "Instance", "parent": "//cloudresourcemanager.googleapis.com/projects/1074106860578", "data": {"fileShares":[{"capacityGb": "1024", "name": "test"}], "name": "projects/tribe29-check-development/locations/us-central1-a/instances/test", "createTime": "2022-03-21T08:14:23.899938334Z", "tier": "BASIC_HDD", "labels": {}, "state": "READY", "networks": [{"modes": ["MODE_IPV4"], "network": "default", "reservedIpRange": "10.212.4.208/29", "ipAddresses": ["10.212.4.210"], "connectMode": "DIRECT_PEERING"}]}, "location": "us-central1-a", "resource_url": ""}, "ancestors": ["projects/1074106860578", "folders/1022571519427", "organizations/668598212003"], "update_time": "2022-03-21T08:18:19.508418Z", "org_policy": []}' ], ] asset_section = gcp.parse_assets(asset_table) servers = list( discover(section_gcp_service_filestore=None, section_gcp_assets=asset_section)) labels = servers[0].labels assert set(labels) == { ServiceLabel("gcp/location", "us-central1-a"), ServiceLabel("gcp/filestore/name", "test"), ServiceLabel("gcp/projectId", "backup-255820"), }
def test_discover_labels_labels_without_user_labels(): asset_table = [ ['{"project":"backup-255820"}'], [ '{"name": "//cloudsql.googleapis.com/projects/tribe29-check-development/instances/checktest", "asset_type": "sqladmin.googleapis.com/Instance", "resource": {"version": "v1beta4", "discovery_document_uri": "https://www.googleapis.com/discovery/v1/apis/sqladmin/v1beta4/rest", "discovery_name": "DatabaseInstance", "parent": "//cloudresourcemanager.googleapis.com/projects/1074106860578", "data": {"serviceAccountEmailAddress": "*****@*****.**", "instanceType": "CLOUDSQL_INSTANCE", "settings": {"dataDiskSizeGb": "20", "kind": "sql#settings", "storageAutoResize": true, "availabilityType": "ZONAL", "settingsVersion": "1", "backupConfiguration": {"kind": "sql#backupConfiguration", "backupRetentionSettings": {"retainedBackups": 7.0, "retentionUnit": "COUNT"}, "startTime": "01:00", "enabled": true, "transactionLogRetentionDays": 7.0, "binaryLogEnabled": false, "location": "us"}, "activationPolicy": "ALWAYS", "replicationType": "SYNCHRONOUS", "pricingPlan": "PER_USE", "locationPreference": {"kind": "sql#locationPreference", "zone": "us-central1-f"}, "storageAutoResizeLimit": "0", "dataDiskType": "PD_HDD", "ipConfiguration": {"ipv4Enabled": true}, "tier": "db-custom-4-26624", "maintenanceWindow": {"hour": 0.0, "day": 0.0, "kind": "sql#maintenanceWindow"}}, "ipAddresses": [{"ipAddress": "34.121.172.190", "type": "PRIMARY"}], "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/tribe29-check-development/instances/checktest", "region": "us-central1", "backendType": "SECOND_GEN", "databaseInstalledVersion": "MYSQL_5_7_36", "createTime": "2022-03-15T08:48:13.998Z", "connectionName": "tribe29-check-development:us-central1:checktest", "kind": "sql#instance", "serverCaCert": {"expirationTime": "2032-03-12T08:51:12.19Z", "kind": "sql#sslCert", "certSerialNumber": "0", "instance": "checktest", "sha1Fingerprint": "05e6c602375a78bd86ca46d9b80709d9bb43a0f2", "createTime": "2022-03-15T08:50:12.19Z", "commonName": "C=US,O=Google\\\\, Inc,CN=Google Cloud SQL Server CA,dnQualifier=8c6bc987-8655-4ff1-aebc-01d408409866"}, "databaseVersion": "MYSQL_5_7", "gceZone": "us-central1-f", "project": "tribe29-check-development", "state": "RUNNABLE", "name": "checktest"}, "location": "us-central1", "resource_url": ""}, "ancestors": ["projects/1074106860578", "folders/1022571519427", "organizations/668598212003"], "update_time": "2022-03-15T08:53:30.997492Z", "org_policy": []}' ], ] asset_section = gcp.parse_assets(asset_table) servers = list( discover(section_gcp_service_cloud_sql=None, section_gcp_assets=asset_section)) labels = servers[0].labels assert set(labels) == { ServiceLabel("gcp/cloud_sql/name", "checktest"), ServiceLabel("gcp/cloud_sql/databaseVersion", "MYSQL_5_7"), ServiceLabel("gcp/cloud_sql/availability", "ZONAL"), ServiceLabel("gcp/location", "us-central1"), ServiceLabel("gcp/projectId", "backup-255820"), }
def test_service_features(): service = Service( item="thingy", parameters={"size": 42}, labels=[ServiceLabel("test-thing", "true")], ) assert service.item == "thingy" assert service.parameters == {"size": 42} assert service.labels == [ServiceLabel("test-thing", "true")] assert repr(service) == ("Service(item='thingy', parameters={'size': 42}," " labels=[ServiceLabel('test-thing', 'true')])") service = Service() assert service.item is None assert service.parameters == {} assert service.labels == [] assert repr(service) == "Service(item=None, parameters={}, labels=[])"
def expected_labels(self) -> set[ServiceLabel]: return { ServiceLabel("gcp/labels/reason", "check-development"), ServiceLabel("gcp/cloud_sql/name", "checktest"), ServiceLabel("gcp/cloud_sql/databaseVersion", "MYSQL_5_7"), ServiceLabel("gcp/labels/team", "dev"), ServiceLabel("gcp/cloud_sql/availability", "ZONAL"), ServiceLabel("gcp/location", "us-central1"), ServiceLabel("gcp/projectId", "backup-255820"), }
def discovery_migration_wrapper(section): disco_func = check_info_dict.get("inventory_function") if not callable(disco_func): # never discover: return original_discovery_result = disco_func(section) if not original_discovery_result: return for element in original_discovery_result: if isinstance(element, tuple) and len(element) in (2, 3): item, raw_params = element[0], element[-1] if item is not None and not isinstance(item, str): raise ValueError("item must be None or of type `str`") parameters = _resolve_string_parameters( raw_params, check_name, get_check_context) service = Service( item=None, # will be replaced parameters=wrap_parameters(parameters or {}), ) # nasty hack for nasty plugins: item = '' # Bypass validation. Item should be None or non-empty string! service = service._replace(item=item) yield service continue with suppress(AttributeError): yield Service( item=element.item, parameters=wrap_parameters(element.parameters or {}), labels=[ ServiceLabel(l.name, l.value) for l in element.service_labels ], ) continue # just let it through. Base must deal with bogus return types anyway. yield element
def test_discover_project_label(self, services: Sequence[Service]): for asset in services: assert ServiceLabel("gcp/projectId", "backup-255820") in asset.labels
def test_service_label(): # as far as the API is concerned, the only important thing ist that they # exist, an can be created like this. _ = ServiceLabel('from-home-office', 'true')
def expected_labels(self) -> set[ServiceLabel]: return { ServiceLabel("gcp/location", "us-central1"), ServiceLabel("gcp/run/name", "aaaa"), ServiceLabel("gcp/projectId", "backup-255820"), }
def test_discover_bucket_labels(functions: Sequence[Service]): labels = functions[0].labels assert len(labels) == 2 assert ServiceLabel("gcp_function_name", "function-2") in labels
def test_discover_project_labels(functions: Sequence[Service]): for bucket in functions: assert ServiceLabel("gcp_project_id", "backup-255820") in bucket.labels
def test_discover_bucket_labels(buckets: Sequence[Service]): labels = buckets[0].labels assert len(labels) == 2 assert ServiceLabel("gcp_bucket_name", "backup-home-ml-free") in labels