
                             d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
  G d d	ej                        Zd
 Zd Zd Zd Zy)z%Base class for Organization commands.    )absolute_import)division)unicode_literals)organizations)
exceptions)	resourcesc                         e Zd Zd fd	Z xZS )UnknownOrganizationErrorc                 P    dj                  |      }t        t        |   ||       y )NzjCannot determine Organization ID from [{0}]. Try `gcloud organizations list` to find your Organization ID.)formatsuperr
   __init__)selforg_argumentmetavarmessage	__class__s       9lib/googlecloudsdk/command_lib/organizations/org_utils.pyr   z!UnknownOrganizationError.__init__   s,    O|$  

"D27GD    )ORGANIZATION_ID)__name__
__module____qualname__r   __classcell__)r   s   @r   r
   r
      s    E Er   r
   c                 H    d}| j                  |      r| t        |      d  S | S )Nzorganizations/)
startswithlen)org_idprefixs     r   StripOrgPrefixr    $   s+    &v#f+,Mr   c                     t         j                  j                         }|j                  dd       t	        | j
                        }|j                  dd|id      }|j                         S )z0Get the Organization URI for the given resource.cloudresourcemanagerv1NorganizationsIdz"cloudresourcemanager.organizations)params
collection)r   REGISTRYCloneRegisterApiByNamer    nameParseSelfLink)resourceregistryr   org_refs       r   OrganizationsUriFuncr0   ,   sk    %%'(
3T:(--(&NN

V 6  7' 
			r   c                     t        j                         }t        |       }|j                         r|j	                  |      S |j                  |      S )a  Get the Organization object for the provided Organization argument.

  Returns the organization object for a given organization ID or will search
  for and return the organization object associated with the given domain name.

  Args:
    org_argument: The value of the organization argument.

  Returns:
    An object representing an organization, or None if the organization could
    not be determined.
  )r   Clientr    isdigitGetGetByDomain)r   orgs_clientr   s      r   GetOrganizationr7   :   sF     $$&+,'&^^??6""""6**r   c                     t        j                         }t        |       }|j                         r|S |j	                  |      }|rt        |j
                        S y)a  Get the Organization ID for the provided Organization argument.

  Numeric values will be returned, values like 'organizations/123456789' will
  return '123456789' and a value like 'example.com' will search for the
  organization ID associated with that domain.

  Args:
    org_argument: The value of the organization argument.

  Returns:
    A string containing the numeric organization ID, or None if the
    organization ID could not be determined.
  N)r   r2   r    r3   r5   r*   )r   r6   r   
org_objects       r   GetOrganizationIdr:   P   sP     $$&+,'&^^M((0JJOO,,r   N)__doc__
__future__r   r   r   +googlecloudsdk.api_lib.cloudresourcemanagerr   googlecloudsdk.callioper   googlecloudsdk.corer   BadArgumentExceptionr
   r    r0   r7   r:    r   r   <module>rB      sD     , &  ' E . )Ez>> E+,r   