Skip to content

Commit

Permalink
feat: add cloud service 'public ip prefixes'
Browse files Browse the repository at this point in the history
Signed-off-by: ImMin5 <[email protected]>
  • Loading branch information
ImMin5 committed Oct 2, 2024
1 parent 47fdcdd commit 20eb695
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 5 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import logging

from plugin.connector.base import AzureBaseConnector

_LOGGER = logging.getLogger("spaceone")


class PublicIPPrefixesConnector(AzureBaseConnector):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_connect(kwargs.get("secret_data"))

def list_all_public_ip_prefixes(self):
return self.network_client.public_ip_prefixes.list_all()
10 changes: 5 additions & 5 deletions src/plugin/manager/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from .disks import *
from .public_ip_addresses import *
from .sql_databases import *
from .sql_servers import *
from .disks import DisksManager
from .public_ip_addresses import PublicIPAddressesManager
from .public_ip_prefiexes import PublicIPPrefixesManager
from .sql_databases import SQLDatabasesManager
from .sql_servers import SQLServersManager
from .virtual_networks import *
from .application_gatways import *
from .nat_gateways import *
Expand All @@ -19,4 +20,3 @@
from .my_sql_servers import *
from .mysql_sql_flexible_servers import *
from .virtual_machines import *

1 change: 1 addition & 0 deletions src/plugin/manager/public_ip_prefiexes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .ip_address_manager import PublicIPPrefixesManager
114 changes: 114 additions & 0 deletions src/plugin/manager/public_ip_prefiexes/ip_address_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import logging

from spaceone.inventory.plugin.collector.lib import *

from plugin.conf.cloud_service_conf import ICON_URL
from plugin.connector.public_ip_prefixes.public_ip_prefixes_connector import (
PublicIPPrefixesConnector,
)

from plugin.connector.subscriptions.subscriptions_connector import (
SubscriptionsConnector,
)
from plugin.manager.base import AzureBaseManager

_LOGGER = logging.getLogger(__name__)


class PublicIPPrefixesManager(AzureBaseManager):
cloud_service_group = "PublicIPPrefixes"
cloud_service_type = "IPPrefix"
service_code = "/Microsoft.Network/publicIPPrefixes"

def create_cloud_service_type(self):
return make_cloud_service_type(
name=self.cloud_service_type,
group=self.cloud_service_group,
provider=self.provider,
service_code=self.service_code,
metadata_path=self.get_metadata_path(),
is_primary=True,
is_major=True,
labels=["Networking"],
tags={"spaceone:icon": f"{ICON_URL}/azure-public-ip-prefix.svg"},
)

def create_cloud_service(self, options, secret_data, schema):
cloud_services = []
error_responses = []

public_ip_prefixes_conn = PublicIPPrefixesConnector(secret_data=secret_data)
subscription_conn = SubscriptionsConnector(secret_data=secret_data)

subscription_obj = subscription_conn.get_subscription(
secret_data["subscription_id"]
)
subscription_info = self.convert_nested_dictionary(subscription_obj)

public_ip_prefixes_list = public_ip_prefixes_conn.list_all_public_ip_prefixes()

for ip_address in public_ip_prefixes_list:

try:
ip_prefix_dict = self.convert_nested_dictionary(ip_address)
ip_address_id = ip_prefix_dict["id"]

ip_prefix_dict = self.update_tenant_id_from_secret_data(
ip_prefix_dict, secret_data
)
ip_prefix_dict.update(
{
"resource_group": self.get_resource_group_from_id(
ip_address_id
),
"subscription_id": subscription_info["subscription_id"],
"subscription_name": subscription_info["display_name"],
"azure_monitor": {"resource_id": ip_address_id},
}
)

prefix_length = ip_prefix_dict.get("prefix_length")
available_ip_address_count = self._get_available_ip_address_count(
prefix_length
)

ip_prefix_dict["available_ip_address_count_display"] = (
available_ip_address_count
)

self.set_region_code(ip_prefix_dict["location"])

cloud_services.append(
make_cloud_service(
name=ip_prefix_dict["name"],
cloud_service_type=self.cloud_service_type,
cloud_service_group=self.cloud_service_group,
provider=self.provider,
data=ip_prefix_dict,
account=secret_data["subscription_id"],
instance_type=ip_prefix_dict["sku"]["name"],
tags=ip_prefix_dict.get("tags", {}),
region_code=ip_prefix_dict["location"],
reference=self.make_reference(ip_prefix_dict.get("id")),
data_format="dict",
)
)

except Exception as e:
_LOGGER.error(
f"[create_cloud_service] Error {self.service} {e}", exc_info=True
)
error_responses.append(
make_error_response(
error=e,
provider=self.provider,
cloud_service_group=self.cloud_service_group,
cloud_service_type=self.cloud_service_type,
)
)

return cloud_services, error_responses

@staticmethod
def _get_available_ip_address_count(prefix_length: int) -> int:
return 2 ** (32 - prefix_length)
43 changes: 43 additions & 0 deletions src/plugin/metadata/public_ip_prefixes/ip_prefiex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
search:
fields:
- Subscription ID: account
- Subscription Name: data.subscription_name
- Resource Group: data.resource_group
- Location: data.location
- SKU: data.sku.name
- Tier: data.sku.tier
- IP Prefix: data.ip_prefix
- Public IP Version: data.public_ip_address_version

table:
sort:
key: data.id
desc: false
fields:
- Available IPs: data.available_ip_address_count_display
- Resource Group: data.resource_group
- Subscription Name: data.subscription_name
- Subscription ID: account
- SKU: data.sku.name
- SKU Tier: data.sku.tier



tabs.0:
name: Public IP Prefix
type: item
fields:
- Name: name
- Resource ID: data.id
- Subscription: data.subscription_name
- Subscription ID: data.subscription_id
- Resource Group: data.resource_group
- Location: data.location
- SKU: data.sku.name
- SKU Tier: data.sku.tier
- IP Prefix: data.ip_prefix
- Provisioning State: data.provisioning_state
- Public IP Version: data.public_ip_address_version
- Available IPs: data.available_ip_address_count_display
- Prefix Length: data.prefix_length

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
metric_id: metric-azure-pubipprefixes-ipprefix-count
name: IP Address Count
metric_type: GAUGE
resource_type: inventory.CloudService:azure.PublicIPPrefixes.IPPrefix
query_options:
group_by:
- key: region_code
name: Region
reference:
resource_type: inventory.Region
reference_key: region_code
- key: data.sku.name
name: Sku name
- key: data.sku.tier
name: Sku Tier
- key: data.tenant_id
name: Tenant ID
- key: data.subscription_name
name: Subscription Name
default: true
- key: account
name: Subscription ID
- key: data.resource_group
name: Resource Group
- key: data.provisioning_state
name: Provisioning State
- key: data.public_ip_address_version
name: Public IP Address Version
fields:
value:
operator: count
unit: Count
namespace_id: ns-azure-pubipprefixes-ipprefixes
version: '1.0'
8 changes: 8 additions & 0 deletions src/plugin/metrics/PublicIPPrefixes/IPAddress/namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
namespace_id: ns-azure-pubipprefixes-ipprefix
name: PublicIPPrefixes/IPPrefix
resource_type: inventory.CloudService:azure.PublicIPPrefixes.IPPrefix
group: azure
category: ASSET
icon: https://spaceone-custom-assets.s3.ap-northeast-2.amazonaws.com/console-assets/icons/cloud-services/azure/azure-public-ip-prefix.svg
version: '1.0'

0 comments on commit 20eb695

Please sign in to comment.