Esempio n. 1
0
    def CreateUser(self, user, owner_email, description=None):
        """Creates an account service user."""

        user = self.clouduseraccounts.MESSAGES_MODULE.User(
            name=user,
            description=description,
            owner=owner_email,
        )

        request = (self.clouduseraccounts.users, 'Insert',
                   self.clouduseraccounts.MESSAGES_MODULE.
                   ClouduseraccountsUsersInsertRequest(project=self.project,
                                                       user=user))

        errors = []
        res = list(
            request_helper.MakeRequests(requests=[request],
                                        http=self.http,
                                        batch_url=self.batch_url,
                                        errors=errors,
                                        custom_get_requests=None))
        if errors:
            utils.RaiseException(errors,
                                 UserException,
                                 error_message='Could not create user:')
        return res
Esempio n. 2
0
def AssertInstanceGroupManagerExists(igm_ref, project, messages, compute, http,
                                     batch_url):
    """Makes sure the given Instance Group Manager exists.

  Args:
    igm_ref: reference to the Instance Group Manager.
    project: project owning resources.
    messages: module containing message classes.
    compute: module representing compute api.
    http: communication channel.
    batch_url: batch url.
  """
    request = messages.ComputeInstanceGroupManagersGetRequest(project=project)
    request.zone = igm_ref.zone
    request.instanceGroupManager = igm_ref.Name()

    errors = []
    # Run throught the generator to actually make the requests and get potential
    # errors.
    igm_details = list(
        request_helper.MakeRequests(
            requests=[(compute.instanceGroupManagers, 'Get', request)],
            http=http,
            batch_url=batch_url,
            errors=errors,
            custom_get_requests=None,
        ))

    if errors or len(igm_details) != 1:
        utils.RaiseException(errors,
                             ResourceNotFoundException,
                             error_message='Could not fetch resource:')
Esempio n. 3
0
    def UploadPublicKey(self, user, public_key):
        """Uploads a public key to a user."""

        description = 'Added by gcloud compute from {0}'.format(
            self.GetHostName())
        default_expiration = '1d'
        parser = arg_parsers.Duration()
        expiration_rfc3339_str = time_utils.CalculateExpiration(
            parser(default_expiration))

        request = self.PublicKeyGenerateUploadRequest(
            user,
            public_key,
            expiration_time=expiration_rfc3339_str,
            description=description)
        requests = [request]

        errors = []
        res = list(
            request_helper.MakeRequests(requests=requests,
                                        http=self.http,
                                        batch_url=self.batch_url,
                                        errors=errors,
                                        custom_get_requests=None))
        if errors:
            utils.RaiseException(errors,
                                 UserException,
                                 error_message='Could not upload public key:')
        return res
Esempio n. 4
0
    def GetOwnerAccounts(self, owners):
        """Look up all users on the current project owned by the list of owners."""
        requests = []
        for owner in owners:
            requests += lister.FormatListRequests(self.service, self.project,
                                                  None, None,
                                                  'owner eq ' + owner)
        errors = []
        responses = request_helper.MakeRequests(requests=requests,
                                                http=self.http,
                                                batch_url=self.batch_url,
                                                errors=errors,
                                                custom_get_requests=None)

        if errors:
            utils.RaiseException(
                errors,
                user_utils.UserException,
                error_message=('Could not get users for owners:'))
        return [response.name for response in responses]
    def _GetGroupFingerprint(self, name, zone):
        """Gets fingerprint of given instance group."""
        get_request = self.messages.ComputeInstanceGroupsGetRequest(
            instanceGroup=name, zone=zone, project=self.project)

        errors = []
        resources = list(
            request_helper.MakeRequests(requests=[(self.compute.instanceGroups,
                                                   'Get', get_request)],
                                        http=self.http,
                                        batch_url=self.batch_url,
                                        errors=errors,
                                        custom_get_requests=None))

        if errors:
            utils.RaiseException(
                errors,
                FingerprintFetchException,
                error_message='Could not set named ports for resource:')
        return resources[0].fingerprint
Esempio n. 6
0
    def LookupUsers(self, users):
        """Makes a get request for each user in users and returns results."""
        requests = []
        for user in users:
            request = (self.clouduseraccounts.users, 'Get',
                       self.clouduseraccounts.MESSAGES_MODULE.
                       ClouduseraccountsUsersGetRequest(project=self.project,
                                                        user=user))
            requests.append(request)

        errors = []
        res = list(
            request_helper.MakeRequests(requests=requests,
                                        http=self.http,
                                        batch_url=self.batch_url,
                                        errors=errors,
                                        custom_get_requests=None))
        if errors:
            utils.RaiseException(errors,
                                 UserException,
                                 error_message='Could not fetch some users:')
        return res
Esempio n. 7
0
    def RemovePublicKey(self, user, fingerprint):
        """Removes a public key from a user."""
        request = (self.clouduseraccounts.users, 'RemovePublicKey',
                   self.clouduseraccounts.MESSAGES_MODULE.
                   ClouduseraccountsUsersRemovePublicKeyRequest(
                       project=self.project,
                       fingerprint=fingerprint,
                       user=user))

        requests = [request]

        errors = []
        res = list(
            request_helper.MakeRequests(requests=requests,
                                        http=self.http,
                                        batch_url=self.batch_url,
                                        errors=errors,
                                        custom_get_requests=None))
        if errors:
            utils.RaiseException(errors,
                                 UserException,
                                 error_message='Could not remove public key:')
        return res