dimanche 2 janvier 2022

Python:Need advice on the design of my small project

So my requirement is to write a script for creaing a VM in Google Cloud Platform. I have gotten all the requirements and determined all the must to have parameters for create_vm API. The inputs can be broadly classified into following sections (both as per the API and as per my requirement.)

  1. Boot Disk Details
  2. Other Disk Details
  3. Network Details
  4. Additional Details
  5. VM Machine Details

So for this i have planned to implement it using python class as below.

 class CreateVM:

def __init__():
    //initialize the template
    pass

def ValidateMachineDetails(self):
    try:
        //call relevant list apis
    except HTTP Error:
        pass
    else:
        // Update other VM machine details parameters in the template
    
    
def BootDiskDetails(self, <parameters for Boot Disk Part>):
    try:
        //call relevant list apis
    except HTTP Error:
        pass
    else:
        // Update boot disk details parameters in the template
        
def OtherDiskDetails(self, <parameters for other disk details>):
    try:
        //call relevant list apis
    except HTTP Error:
        pass
    else:
        // Update other disk details parameters in the template
        
def NetworkDetailsDetails(self, <parameters for network details>):
    try:
        //call relevant list apis
    except HTTP Error:
        pass
    else:
        // Update network details parameters in the template

def AdditionalDetails(self, <parameters for AdditionalDetails):
    try:
        //call relevant list apis
    except HTTP Error:
        pass
    else:
        // Update additional details parameters in the template

I have following questions

  1. Is this solution better implemented with only function or is it correct design to use the classes for this solution?
  2. If so is my design of the class a correct approach?
  3. If not, what should i do to improve my apprach?

The base template which i am update is as below:

{

        "name": "",  # Mandatory
        "tags": {
            "items": [],
        },
        "machineType": "",  # Mandatory

        "canIpForward": False,
        "networkInterfaces": [
            {
                "network": "",
                "subnetwork": "",
                "networkIP": "",
                "name": "",
                "accessConfigs": [
                    {
                        "name": 'External NAT',
                        "natIP": "",
                    }
                ],

                "aliasIpRanges": [],  # Needed only if CIDR Blocks are added
                "stackType": 'IPV4_ONLY',
            }
        ],
        "disks": [
            {
                "type": '',
                "mode": '',
                "source": '',  # Only for persistent disks
                "deviceName": '',  # Only for persistent disks

                "boot": False,
                "initializeParams": {
                    "diskName": '',
                    'sourceImage': '',
                    "diskSizeGb": 0,
                    "diskType": '',
                    "sourceImageEncryptionKey": {
                        "rawKey": '',  # Either this key or rsaEncryptedKey must be provided
                        "rsaEncryptedKey": '',
                        "kmsKeyName": '',
                        "kmsKeyServiceAccount": ''
                        # If not provided compute engine default service account is used.
                    },
                    "labels": {
                        '': '',  # Key Value pair only available for persistent disks

                    },
                    "sourceSnapshot": '',
                    "sourceSnapshotEncryptionKey": {
                        "rawKey": '',  # Either this key or rsaEncryptedKey must be provided
                        "rsaEncryptedKey": '',
                        "kmsKeyName": '',
                        "kmsKeyServiceAccount": ''
                        # If not provided compute engine default service account is used.
                    },

                },
                "autoDelete": False,

                "interface": '',

                "diskEncryptionKey": {
                    "kmsKeyServiceAccount": '',
                    "rawKey": '',
                    "rsaEncryptedKey": '',
                    "kmsKeyName": ''

                },
                "diskSizeGb": '',

            }
        ],
        "metadata": {

            "items": [
                {
                    "key": '',
                    "value": ''
                }
            ],
        },
        "serviceAccounts": [
            {
                "email": '',
                "scopes": []
            }
        ],
        "scheduling": {
            "onHostMaintenance": 'MIGRATE',
            "automaticRestart": True,
            "preemptible": False,
            "nodeAffinities": [
                {
                    "key": '',
                    "operator": '',
                    "values": []
                }
            ],

        },
        "cpuPlatform": '',
        "labels": {
        },

        "guestAccelerators": [
            {
                "acceleratorType": '',
                "acceleratorCount": 0
            }
        ],

        "deletionProtection": False,
        "resourcePolicies": [],  # Array of Strings
        "reservationAffinity": {
            "consumeReservationType": '',

        },
        "hostname": '',
        "displayDevice": {
            "enableDisplay": False
        },
        "shieldedInstanceConfig": {
            "enableSecureBoot": False,
            "enableVtpm": True,
            "enableIntegrityMonitoring": True
        },

        "confidentialInstanceConfig": {
            "enableConfidentialCompute": False
        },
        "privateIpv6GoogleAccess": 'INHERIT_FROM_SUBNETWORK ',
        "advancedMachineFeatures": {
            "enableNestedVirtualization": False,
        },

        "networkPerformanceConfig": {
            "totalEgressBandwidthTier": ""
        },
        "kind": ""
    }

Aucun commentaire:

Enregistrer un commentaire