Skip to content

Detect specific supported cloud providers  #22

@dwc0011

Description

@dwc0011

Cloud detect has the capability to detect multiple cloud providers, however some applications may only support a few cloud providers and trying to detect the others is a waste of time and resources. I propose accepting a list of cloud providers that a user wants to detect.

I have attached sample code below but would me more than willing to create a PR to implement this if you are open to this feature request.

For example an application may support running on AWS, Azure, or GCP but nothing else. The application would only care if it is one of those so attempting to detect others makes no sense to the using app.

A possible solution while keeping it dynamic would be to modify the cloud_detect/init.py class so that the usage would look like the sample script and the init.py would look like the code below the sample script.

#sample.py
from cloud_detect import provider

MY_APP_PROVIDERS = ['aws', 'azure', 'gcp']

def do_aws_work(p_id):
  print(p_id == "aws")

def do_azure_work(p_id):
  print(p_id == "azure")

def do_gcp_work(p_id):
  print(p_id == "gcp")

def error(id):
  print("Error unknown id = " + id)
  
MY_APP_PROVIDERS = {
      'aws': do_aws_work, 
      'azure': do_azure_work, 
      'gcp': do_gcp_work, 
}
        
def detect_env():
       only_these = [key for key in MY_APP_PROVIDERS]
       provider_id  = provider(only_these)
       
       MY_APP_PROVIDERS.get(provider_id, error)(provider_id)
#cloud_detect/__init__.py
__PROVIDER_CLASSES = {
    AlibabaProvider.identifier: AlibabaProvider, 
    AWSProvider.identifier :AWSProvider, 
    AzureProvider.identifier: AzureProvider,
    DOProvider.identifier: DOProvider,
    GCPProvider.identifier: GCPProvider,
    OCIProvider.identifier: OCIProvider
}

async def _identify(timeout, providers= None):

     if not providers:
        providers = [identifier for identifier in __PROVIDER_CLASSES]
       ......
       ......
       tasks = {
        __PROVIDER_CLASSES[p_id].identifier : asyncio.ensure_future(wrapper(__PROVIDER_CLASSES[p_id])) for p_id in providers if p_id in __PROVIDER_CLASSES
    }
        ......

def provider(timeout=None, providers= None,):
     .....
     .....
     if py_version.minor >= 7:
        result = asyncio.run(_identify(timeout, providers))
    else:
        loop = asyncio.new_event_loop()
        result = loop.run_until_complete(_identify(timeout, providers))
        loop.close()
    return result
........

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions