Skip to content

Commit

Permalink
[azure][feat] Add support for sql resource collection (#2144)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Veit <[email protected]>
  • Loading branch information
1101-1 and aquamatthias authored Jul 19, 2024
1 parent 75133b9 commit 4f4d80e
Show file tree
Hide file tree
Showing 22 changed files with 2,215 additions and 19 deletions.
2 changes: 2 additions & 0 deletions plugins/azure/fix_plugin_azure/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
AzureNetworkUsage,
resources as network_resources,
)
from fix_plugin_azure.resource.sql import resources as sql_resources
from fix_plugin_azure.resource.storage import AzureStorageAccountUsage, AzureStorageSku, resources as storage_resources
from fixlib.baseresources import Cloud, GraphRoot, BaseAccount, BaseRegion
from fixlib.core.actions import CoreFeedback, ErrorAccumulator
Expand All @@ -59,6 +60,7 @@ def resource_with_params(clazz: Type[MicrosoftResource], param: str) -> bool:
+ aks_resources
+ security_resources
+ storage_resources
+ sql_resources
)
all_resources = subscription_resources + graph_resources # defines all resource kinds. used in model check

Expand Down
33 changes: 17 additions & 16 deletions plugins/azure/fix_plugin_azure/resource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,35 @@ class MicrosoftResource(BaseResource):
etag: Optional[str] = field(default=None, metadata={'description': 'A unique read-only string that changes whenever the resource is updated.'}) # fmt: skip
provisioning_state: Optional[str] = field(default=None, metadata={'description': 'The current provisioning state.'}) # fmt: skip

@property
def resource_subscription_id(self) -> Optional[str]:
return self.extract_part("subscriptionId")
return self.extract_part("subscriptions")

@property
def resource_group_name(self) -> Optional[str]:
return self.extract_part("resourceGroups")

def extract_part(self, part: str) -> Optional[str]:
"""
Extracts a specific part from a resource ID.
The function takes a resource ID and a specified part to extract, such as 'subscriptionId'.
The function takes a resource ID and a specified part to extract, such as 'subscriptions'.
The resource ID is expected to follow the Azure Resource Manager path format.
Example:
For the resource ID "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/...",
calling extract_part("subscriptionId") would return the value within the curly braces,
representing the subscription ID.
calling extract_part("subscriptions") would return the value representing the subscription ID.
Parameters:
- part (str): The part to extract from the resource ID.
Returns:
str: The extracted part of the resource ID.
Optional[str]: The extracted part of the resource ID, or None if not found.
"""
id_parts = self.id.split("/")

if part == "subscriptionId":
if "subscriptions" not in id_parts:
return None
if index := id_parts.index("subscriptions"):
return id_parts[index + 1]
return None
else:
try:
return id_parts[id_parts.index(part) + 1]
except ValueError:
return None

def delete(self, graph: Graph) -> bool:
Expand All @@ -101,7 +100,7 @@ def delete(self, graph: Graph) -> bool:
Returns:
bool: True if the resource was successfully deleted; False otherwise.
"""
subscription_id = self.resource_subscription_id()
subscription_id = self.resource_subscription_id
if subscription_id is None:
log.warning("Failed to delete resource. Subscription ID is not available.")
return False
Expand All @@ -113,7 +112,7 @@ def delete_tag(self, key: str) -> bool:
This method removes a specific value from a tag associated with a subscription, while keeping the tag itself intact.
The tag remains on the account, but the specified value will be deleted.
"""
subscription_id = self.resource_subscription_id()
subscription_id = self.resource_subscription_id
if subscription_id is None:
log.warning("Failed to delete tag. Subscription ID is not available.")
return False
Expand All @@ -125,7 +124,7 @@ def update_tag(self, key: str, value: str) -> bool:
This method allows for the creation or update of a tag value associated with the specified tag name.
The tag name must already exist for the operation to be successful.
"""
subscription_id = self.resource_subscription_id()
subscription_id = self.resource_subscription_id
if subscription_id is None:
log.warning("Failed to update tag. Subscription ID is not available.")
return False
Expand Down Expand Up @@ -570,11 +569,13 @@ class AzureSku:
"name": S("name"),
"tier": S("tier"),
"family": S("family"),
"size": S("size"),
}
capacity: Optional[int] = field(default=None, metadata={'description': 'Specifies the number of virtual machines in the scale set.'}) # fmt: skip
family: Optional[str] = field(default=None, metadata={"description": "The family of the sku."})
name: Optional[str] = field(default=None, metadata={"description": "The sku name."})
tier: Optional[str] = field(default=None, metadata={'description': 'Specifies the tier of virtual machines in a scale set. Possible values: **standard** **basic**.'}) # fmt: skip
size: Optional[str] = field(default=None, metadata={"description": "Size of the particular SKU"})


class GraphBuilder:
Expand Down
2 changes: 1 addition & 1 deletion plugins/azure/fix_plugin_azure/resource/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from fix_plugin_azure.azure_client import AzureResourceSpec
from fix_plugin_azure.resource.base import (
AzureBaseUsage,
MicrosoftResource,
GraphBuilder,
AzureSubResource,
AzureSku,
AzureExtendedLocation,
AzurePrivateLinkServiceConnectionState,
AzureManagedServiceIdentity,
MicrosoftResource,
)
from fix_plugin_azure.resource.containerservice import AzureManagedCluster
from fix_plugin_azure.utils import rgetattr
Expand Down
Loading

0 comments on commit 4f4d80e

Please sign in to comment.