Exemplo n.º 1
0
 def Run(self, args):
     service = folders.FoldersService()
     messages = folders.FoldersMessages()
     result = service.Delete(
         messages.CloudresourcemanagerFoldersDeleteRequest(
             foldersId=args.id))
     log.DeletedResource(result)
Exemplo n.º 2
0
 def Run(self, args):
     folder = folders.GetFolder(args.id)
     folder.displayName = args.display_name
     update = folders.FoldersMessages(
     ).CloudresourcemanagerFoldersUpdateRequest(folder=folder,
                                                foldersId=args.id)
     log.UpdatedResource(folders.FoldersService().Update(update))
Exemplo n.º 3
0
 def Run(self, args):
     folder = folders.GetFolder(args.id)
     folder.displayName = args.display_name
     request = folders.FoldersMessages(
     ).CloudresourcemanagerFoldersPatchRequest(folder=folder,
                                               foldersId=args.id,
                                               updateMask='display_name')
     log.UpdatedResource(folders.FoldersService().Patch(request))
Exemplo n.º 4
0
 def SetUpFolderMove(self, original, moved, is_async=False):
     response_value = operations.ToOperationResponse(moved)
     test_move_operation = self.messages.Operation(done=False,
                                                   name=TEST_OPERATION_NAME)
     expected_move_request = folders.FoldersMessages(
     ).CloudresourcemanagerFoldersMoveRequest(
         foldersId=folders.FolderNameToId(original.name),
         moveFolderRequest=folders.FoldersMessages().MoveFolderRequest(
             destinationParent=moved.parent))
     self.mock_folders.Move.Expect(expected_move_request,
                                   test_move_operation)
     if not is_async:
         test_move_operation_done = operations.OperationsMessages(
         ).Operation(done=True,
                     name=TEST_OPERATION_NAME,
                     response=response_value)
         self.mock_operations.Get.Expect(
             self.ExpectedGetOperationRequest(test_move_operation),
             test_move_operation_done)
Exemplo n.º 5
0
 def Run(self, args):
   """Run the list command."""
   flags.CheckParentFlags(args)
   return list_pager.YieldFromList(
       folders.FoldersService(),
       folders.FoldersMessages().CloudresourcemanagerFoldersListRequest(
           parent=flags.GetParentFromFlags(args)),
       limit=args.limit,
       batch_size_attribute='pageSize',
       batch_size=args.page_size,
       field='folders')
Exemplo n.º 6
0
 def testLimit(self):
   self.mock_folders.List.Expect(
       self.ExpectedListRequest(),
       folders.FoldersMessages().ListFoldersResponse(
           folders=[self.TEST_FOLDER, self.ANOTHER_TEST_FOLDER]))
   self.DoCommand(limit='1')
   self.AssertOutputContains(
       """\
     DISPLAY_NAME             PARENT_NAME          ID
     Test Folder For Testing  organizations/24521  58219052
     """,
       normalize_space=True)
Exemplo n.º 7
0
 def Run(self, args):
     flags.CheckParentFlags(args)
     messages = folders.FoldersMessages()
     operation = folders.FoldersService().Create(
         messages.CloudresourcemanagerFoldersCreateRequest(
             parent=flags.GetParentFromFlags(args),
             folder=messages.Folder(displayName=args.display_name)))
     if args. async:
         return operation
     else:
         finished_operation = operations.WaitForOperation(operation)
         result = operations.ExtractOperationResponse(
             finished_operation, messages.Folder)
         log.CreatedResource(result)
Exemplo n.º 8
0
  def testPagination(self):
    test_token = 'next1'
    # First page
    self.mock_folders.List.Expect(
        folders.FoldersMessages().CloudresourcemanagerFoldersListRequest(
            parent=self.TEST_FOLDER.parent, pageSize=1),
        folders.FoldersMessages().ListFoldersResponse(
            folders=[self.TEST_FOLDER], nextPageToken=test_token))
    # Second page
    self.mock_folders.List.Expect(
        folders.FoldersMessages().CloudresourcemanagerFoldersListRequest(
            parent=self.TEST_FOLDER.parent, pageSize=1, pageToken=test_token),
        folders.FoldersMessages().ListFoldersResponse(
            folders=[self.ANOTHER_TEST_FOLDER]))
    self.DoCommand(page_size='1')
    self.AssertOutputContains(
        """\
      DISPLAY_NAME                     PARENT_NAME          ID
      Test Folder For Testing          organizations/24521  58219052

      DISPLAY_NAME                     PARENT_NAME          ID
      Another Test Folder For Testing  organizations/24521  67045082
      """,
        normalize_space=True)
Exemplo n.º 9
0
 def Run(self, args):
     flags.CheckParentFlags(args)
     messages = folders.FoldersMessages()
     move_request = messages.CloudresourcemanagerFoldersMoveRequest(
         foldersId=args.id,
         moveFolderRequest=messages.MoveFolderRequest(
             destinationParent=flags.GetParentFromFlags(args)))
     operation = folders.FoldersService().Move(move_request)
     if args. async:
         return operation
     else:
         finished_op = operations.WaitForOperation(operation)
         result = operations.ExtractOperationResponse(
             finished_op, messages.Folder)
         log.UpdatedResource(result)
Exemplo n.º 10
0
    def Run(self, args):
        messages = folders.FoldersMessages()
        policy = iam_util.ParsePolicyFile(args.policy_file, messages.Policy)
        update_mask = iam_util.ConstructUpdateMaskFromPolicy(args.policy_file)

        # To preserve the existing set-iam-policy behavior of always overwriting
        # bindings and etag, add bindings and etag to update_mask.
        if 'bindings' not in update_mask:
            update_mask += ',bindings'
        if 'etag' not in update_mask:
            update_mask += ',etag'

        result = folders.SetIamPolicy(args.id, policy, update_mask)
        iam_util.LogSetIamPolicy(args.id, 'folder')
        return result
Exemplo n.º 11
0
class FolderIntegrationTest(e2e_base.WithServiceAuth):

    messages = folders.FoldersMessages()

    def SetUp(self):
        properties.VALUES.core.user_output_enabled.Set(False)

    def testDescribeFolder(self):
        result = self.RunFolders('describe', TEST_TOP_LEVEL_FOLDER_ID)
        self.assertEqual(result, TEST_TOP_LEVEL_FOLDER)

    def testListFoldersInOrganization(self):
        result = self.RunFolders('list', '--organization',
                                 TEST_ORGANIZATION_ID)
        self.assertIn(TEST_TOP_LEVEL_FOLDER, list(result))

    def testListFoldersInFolder(self):
        result = self.RunFolders('list', '--folder', TEST_TOP_LEVEL_FOLDER_ID)
        self.assertIn(TEST_SUBFOLDER_A, list(result))
        self.assertIn(TEST_SUBFOLDER_B, list(result))

    def testSetIamPolicy(self):
        folder_id = folders.FolderNameToId(TEST_SUBFOLDER_B.name)

        new_policy = self.messages.Policy(bindings=[
            self.messages.Binding(members=['serviceAccount:' + self.Account()],
                                  role='roles/owner')
        ],
                                          version=0)

        # Because this policy won't mess up any other integration
        # tests, we don't need to worry about changing it back to
        # the original policy (which is nontrivial to perform safely
        # in an environment where tests can be executed concurrently).
        created_policy = self.setIamPolicy(folder_id, new_policy)

        self.assertListEqual(created_policy.auditConfigs, [])
        self.assertListEqual(created_policy.bindings, new_policy.bindings)

    def setIamPolicy(self, resource_id, policy):
        json = encoding.MessageToJson(policy)
        policy_file_path = self.Touch(self.temp_path,
                                      'good.json',
                                      contents=json)
        return self.RunFolders('set-iam-policy', resource_id, policy_file_path)

    def RunFolders(self, *command):
        return self.Run(['resource-manager', 'folders'] + list(command))
Exemplo n.º 12
0
    def Run(self, args):
        messages = folders.FoldersMessages()
        policy, update_mask = iam_util.ParsePolicyFileWithUpdateMask(
            args.policy_file, messages.Policy)
        policy.version = iam_util.MAX_LIBRARY_IAM_SUPPORTED_VERSION

        # To preserve the existing set-iam-policy behavior of always overwriting
        # bindings and etag, add bindings and etag to update_mask.
        if 'bindings' not in update_mask:
            update_mask += ',bindings'
        if 'etag' not in update_mask:
            update_mask += ',etag'

        result = folders.SetIamPolicy(args.id, policy, update_mask)
        iam_util.LogSetIamPolicy(args.id, 'folder')
        return result
Exemplo n.º 13
0
 def ExpectedListRequest(self):
   return folders.FoldersMessages().CloudresourcemanagerFoldersListRequest(
       parent=self.TEST_FOLDER.parent)
Exemplo n.º 14
0
class FoldersUnitTestBase(CrmUnitTestBase):
  """Base class for all Folders unit tests with fake auth and mocks."""

  messages = folders.FoldersMessages()

  ACTIVE = messages.Folder.LifecycleStateValueValuesEnum.ACTIVE
  DELETED = messages.Folder.LifecycleStateValueValuesEnum.DELETE_REQUESTED

  TEST_FOLDER = messages.Folder(
      name='folders/58219052',
      parent='organizations/24521',
      displayName='Test Folder For Testing',
      lifecycleState=ACTIVE)

  ANOTHER_TEST_FOLDER = messages.Folder(
      name='folders/67045082',
      parent='organizations/24521',
      displayName='Another Test Folder For Testing',
      lifecycleState=ACTIVE)

  UPDATED_TEST_FOLDER = messages.Folder(
      name='folders/58219052',
      parent='organizations/24521',
      displayName='Test Folder Updated To Be More Awesome',
      lifecycleState=ACTIVE)

  DELETED_TEST_FOLDER = messages.Folder(
      name='folders/58219052',
      parent='organizations/24521',
      displayName='Test Folder For Testing',
      lifecycleState=DELETED)

  TEST_FOLDER_WITH_FOLDER_PARENT = messages.Folder(
      name='folders/58219052',
      parent='folders/12345',
      displayName='Test Folder For Testing',
      lifecycleState=ACTIVE)

  def SetUp(self):
    self.track = calliope_base.ReleaseTrack.GA

  def _GetTestIamPolicy(self, clear_fields=None):
    """Creates a test IAM policy.

    Args:
        clear_fields: list of policy fields to clear.
    Returns:
        IAM policy.
    """
    policy = self.messages.Policy(
        auditConfigs=[
            self.messages.AuditConfig(
                auditLogConfigs=[
                    self.messages.AuditLogConfig(
                        logType=self.messages.AuditLogConfig.
                        LogTypeValueValuesEnum.ADMIN_READ,),
                ],
                service='allServices',)
        ],
        bindings=[
            self.messages.Binding(
                role='roles/resourcemanager.projectCreator',
                members=['domain:foo.com'],),
            self.messages.Binding(
                role='roles/resourcemanager.organizationAdmin',
                members=['user:[email protected]'],),
        ],
        etag=http_encoding.Encode('someUniqueEtag'),
        version=iam_util.MAX_LIBRARY_IAM_SUPPORTED_VERSION,)

    if clear_fields is None:
      clear_fields = []

    for field in clear_fields:
      policy.reset(field)

    return policy

  def RunFolders(self, *command):
    return self.Run(['resource-manager', 'folders'] + list(command))
 def Run(self, args):
   messages = folders.FoldersMessages()
   policy = folders.GetIamPolicy(args.id)
   iam_util.AddBindingToIamPolicy(
       messages.Binding, policy, args.member, args.role)
   return folders.SetIamPolicy(args.id, policy)
Exemplo n.º 16
0
 def ExpectedRequest(self):
     return folders.FoldersMessages(
     ).CloudresourcemanagerFoldersDeleteRequest(
         foldersId=folders.FolderNameToId(self.TEST_FOLDER.name))
Exemplo n.º 17
0
 def testNoFlagsEmptyList(self):
   self.mock_folders.List.Expect(
       self.ExpectedListRequest(),
       folders.FoldersMessages().ListFoldersResponse())
   self.DoCommand()
   self.AssertOutputEquals('', normalize_space=True)
Exemplo n.º 18
0
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

from apitools.base.py import encoding

from googlecloudsdk.api_lib.resource_manager import folders
from googlecloudsdk.core import properties
from tests.lib import e2e_base
from tests.lib import test_case

messages = folders.FoldersMessages()

FOLDER_ACTIVE = messages.Folder.LifecycleStateValueValuesEnum.ACTIVE

TEST_ORGANIZATION_ID = '1054311078602'  # cloudsdktest.joonix.net
TEST_TOP_LEVEL_FOLDER_ID = '1095953820273'  # Resource Manager Test Folder
TEST_TOP_LEVEL_FOLDER_NAME = 'folders/{0}'.format(TEST_TOP_LEVEL_FOLDER_ID)

TEST_TOP_LEVEL_FOLDER = messages.Folder(
    name=TEST_TOP_LEVEL_FOLDER_NAME,
    parent='organizations/{0}'.format(TEST_ORGANIZATION_ID),
    displayName='Resource Manager Test Folder',
    lifecycleState=FOLDER_ACTIVE,
    createTime='2019-08-30T16:10:52.243Z')

TEST_SUBFOLDER_A = messages.Folder(createTime='2019-08-30T16:26:57.750Z',
Exemplo n.º 19
0
 def ExpectedPatchRequest(self):
     return folders.FoldersMessages(
     ).CloudresourcemanagerFoldersPatchRequest(
         folder=self.UPDATED_TEST_FOLDER,
         foldersId=folders.FolderNameToId(self.UPDATED_TEST_FOLDER.name),
         updateMask='display_name')
Exemplo n.º 20
0
 def ExpectedCreateRequest(self):
     messages = folders.FoldersMessages()
     return messages.CloudresourcemanagerFoldersCreateRequest(
         parent=self.TEST_FOLDER.parent,
         folder=messages.Folder(displayName=self.TEST_FOLDER.displayName))
Exemplo n.º 21
0
 def Run(self, args):
     messages = folders.FoldersMessages()
     policy = iam_util.ParsePolicyFile(args.policy_file, messages.Policy)
     return folders.SetIamPolicy(args.id, policy)