예제 #1
0
    def testSetFailIfPresentSucceeds(self):
        # Tests that --fail-if-present doesn't cause an error if the named
        # variable does not already exist.
        config_name = 'projects/{0}/configs/foo'.format(self.Project())
        var_name = 'projects/{0}/configs/foo/variables/var1'.format(
            self.Project())
        request = self.messages.RuntimeconfigProjectsConfigsVariablesCreateRequest(
            parent=config_name,
            variable=self.messages.Variable(
                name=var_name,
                value=b'value1',
            ),
        )
        wanted_result = self.messages.Variable(
            name=var_name,
            updateTime='2016-04-16T00:00:00Z',
            value=b'value1',
        )

        self.variable_client.Create.Expect(request, wanted_result)
        got_result = self.RunRuntimeConfig(
            'variables set var1 "value1" --config-name foo '
            '--fail-if-present')

        self.assertEqual(util.FormatVariable(wanted_result), got_result)
예제 #2
0
    def testListCustomPageSize(self):
        request = self.messages.RuntimeconfigProjectsConfigsVariablesListRequest(
            parent='projects/{0}/configs/foo'.format(self.Project()),
            pageSize=55,
            returnValues=False,
        )

        variables = [
            self.messages.Variable(
                name='projects/{0}/configs/foo/variables/var1'.format(
                    self.Project()),
                updateTime='2016-04-16T00:00:00Z',
            ),
            self.messages.Variable(
                name='projects/{0}/configs/foo/variables/var2/var3'.format(
                    self.Project()),
                updateTime='2016-04-16T01:00:00Z',
            ),
        ]
        wrapped_result = self.messages.ListVariablesResponse(
            variables=variables,
            nextPageToken=None,
        )

        self.variable_client.List.Expect(request, wrapped_result)
        got_result = self.RunRuntimeConfig(
            'variables list --config-name foo --page-size 55')

        self.assertEqual([util.FormatVariable(v) for v in variables],
                         list(got_result))
예제 #3
0
    def testSetOverwritesExisting(self):
        config_name = 'projects/{0}/configs/foo'.format(self.Project())
        var_name = 'projects/{0}/configs/foo/variables/var1'.format(
            self.Project())

        # Short names due to the long class names hitting 80 characters
        cr_req = self.messages.RuntimeconfigProjectsConfigsVariablesCreateRequest(
            parent=config_name,
            variable=self.messages.Variable(
                name=var_name,
                value=b'value1',
            ),
        )
        cr_exception = base.MakeHttpError(409, 'ALREADY_EXISTS')

        upd_req = self.messages.Variable(
            name=var_name,
            value=b'value1',
        )
        upd_result = self.messages.Variable(
            name=var_name,
            state=self.messages.Variable.StateValueValuesEnum.UPDATED,
            updateTime='2016-04-16T00:00:00Z',
            value=b'value1',
        )

        self.variable_client.Create.Expect(cr_req, exception=cr_exception)
        self.variable_client.Update.Expect(upd_req, upd_result)
        got_result = self.RunRuntimeConfig(
            'variables set var1 "value1" --config-name foo')

        self.assertEqual(util.FormatVariable(upd_result), got_result)
예제 #4
0
  def Run(self, args):
    """Run 'runtime-configs variables list'.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Yields:
      The list of variables.

    Raises:
      HttpException: An http error response was received while executing api
          request.
    """
    variable_client = util.VariableClient()
    messages = util.Messages()

    config_resource = util.ParseConfigName(util.ConfigName(args))

    self._display_values = args.values

    request = messages.RuntimeconfigProjectsConfigsVariablesListRequest(
        parent=config_resource.RelativeName(),
        returnValues=self._display_values)

    page_size = args.page_size or self.DEFAULT_PAGE_SIZE

    results = list_pager.YieldFromList(
        variable_client, request, field='variables',
        batch_size_attribute='pageSize', limit=args.limit,
        batch_size=page_size
    )

    for result in results:
      yield util.FormatVariable(result, self._display_values)
예제 #5
0
    def Run(self, args):
        """Run a command that watches a variable.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      The WatchVariable response.

    Raises:
      HttpException: An http error response was received while executing api
          request.
    """
        # Disable retries and configure the timeout.
        variable_client = util.VariableClient(num_retries=0,
                                              timeout=args.max_wait)
        messages = util.Messages()

        var_resource = util.ParseVariableName(args.name, args)

        if args.newer_than:
            newer_than = times.FormatDateTime(args.newer_than)
        else:
            newer_than = None

        with progress_tracker.ProgressTracker(
                'Waiting for variable [{0}] to change'.format(
                    var_resource.Name())):
            try:
                return util.FormatVariable(
                    variable_client.Watch(
                        messages.
                        RuntimeconfigProjectsConfigsVariablesWatchRequest(
                            name=var_resource.RelativeName(),
                            watchVariableRequest=messages.WatchVariableRequest(
                                newerThan=newer_than, ))))

            except apitools_exceptions.HttpError as error:
                # For deadline exceeded or bad gateway errors,
                # we return a status code of 2.
                # In some cases, the GFE will timeout before the backend
                # responds with a 504 Gateway Timeout (DEADLINE_EXCEEDED).
                # In that scenario, GFE responds first with a 502 BAD GATEWAY error.
                if util.IsDeadlineExceededError(
                        error) or util.IsBadGatewayError(error):
                    _RaiseTimeout()
                raise

            except socket.error as error:
                if util.IsSocketTimeout(error):
                    _RaiseTimeout()
                raise
예제 #6
0
    def _Update(self, args, var_resource, value):
        variable_client = util.VariableClient()
        messages = util.Messages()

        result = variable_client.Update(
            messages.Variable(
                name=var_resource.RelativeName(),
                value=value if not args.is_text else None,
                text=value if args.is_text else None,
            ))

        log.UpdatedResource(var_resource)
        return util.FormatVariable(result)
예제 #7
0
 def testFormatVariableDecodeValue(self):
     variable = MESSAGE_MODULE.Variable(
         name='projects/my-project/configs/my-config/variables/my/var',
         value=b'asdf',
         state=MESSAGE_MODULE.Variable.StateValueValuesEnum.UPDATED,
         updateTime='2016-04-29T00:00:00Z',
     )
     munged_variable = {
         'atomicName':
         'projects/my-project/configs/my-config/variables/my/var',
         'name': 'my/var',
         'value': b'asdf',
         'state': 'UPDATED',
         'updateTime': '2016-04-29T00:00:00Z',
     }
     self.assertEqual(munged_variable, util.FormatVariable(variable, True))
예제 #8
0
 def testFormatVariableOutputText(self):
     variable = MESSAGE_MODULE.Variable(
         name='projects/my-project/configs/my-config/variables/my/var',
         text='qwer',
         state=MESSAGE_MODULE.Variable.StateValueValuesEnum.UPDATED,
         updateTime='2016-04-29T00:00:00Z',
     )
     munged_variable = {
         'atomicName':
         'projects/my-project/configs/my-config/variables/my/var',
         'name': 'my/var',
         'value': 'qwer',
         'text': 'qwer',
         'state': 'UPDATED',
         'updateTime': '2016-04-29T00:00:00Z',
     }
     self.assertEqual(util.FormatVariable(variable, True), munged_variable)
예제 #9
0
    def _Create(self, args, var_resource, value):
        variable_client = util.VariableClient()
        messages = util.Messages()

        project = var_resource.projectsId
        config = var_resource.configsId

        result = variable_client.Create(
            messages.RuntimeconfigProjectsConfigsVariablesCreateRequest(
                parent=util.ConfigPath(project, config),
                variable=messages.Variable(
                    name=var_resource.RelativeName(),
                    value=value if not args.is_text else None,
                    text=value if args.is_text else None,
                )))

        log.CreatedResource(var_resource)
        return util.FormatVariable(result)
예제 #10
0
    def testWatchMultiSegmentName(self):
        var_name = 'projects/{0}/configs/foo/variables/var1/var2'.format(
            self.Project())
        request = self.messages.RuntimeconfigProjectsConfigsVariablesWatchRequest(
            name=var_name,
            watchVariableRequest=self.messages.WatchVariableRequest(
                newerThan=None, ))
        wanted_result = self.messages.Variable(
            name=var_name,
            updateTime='2016-04-16T00:00:00Z',
            state=self.messages.Variable.StateValueValuesEnum.UPDATED,
            value=b'value1',
        )

        self.variable_client.Watch.Expect(request, wanted_result)
        got_result = self.RunRuntimeConfig(
            'variables watch var1/var2 --config-name foo')

        self.assertEqual(util.FormatVariable(wanted_result), got_result)
예제 #11
0
    def testList(self):
        # Tests a list request with two pages.
        variables = [
            self.messages.Variable(
                name='projects/{0}/configs/foo/variables/var1'.format(
                    self.Project()),
                updateTime='2016-04-16T00:00:00Z',
            ),
            self.messages.Variable(
                name='projects/{0}/configs/foo/variables/var2/var3'.format(
                    self.Project()),
                updateTime='2016-04-16T01:00:00Z',
            ),
        ]
        request_1 = self.messages.RuntimeconfigProjectsConfigsVariablesListRequest(
            parent='projects/{0}/configs/foo'.format(self.Project()),
            pageSize=self.DEFAULT_PAGE_SIZE,
            returnValues=False,
        )
        request_2 = self.messages.RuntimeconfigProjectsConfigsVariablesListRequest(
            parent='projects/{0}/configs/foo'.format(self.Project()),
            pageSize=self.DEFAULT_PAGE_SIZE,
            pageToken='foobar',
            returnValues=False,
        )

        wrapped_result_1 = self.messages.ListVariablesResponse(
            variables=variables[:1],
            nextPageToken='foobar',
        )
        wrapped_result_2 = self.messages.ListVariablesResponse(
            variables=variables[1:],
            nextPageToken=None,
        )

        self.variable_client.List.Expect(request_1, wrapped_result_1)
        self.variable_client.List.Expect(request_2, wrapped_result_2)
        got_result = self.RunRuntimeConfig('variables list --config-name foo')

        self.assertEqual([util.FormatVariable(v) for v in variables],
                         list(got_result))
예제 #12
0
    def testSetFailIfAbsentSucceeds(self):
        # Tests that --fail-if-absent doesn't cause an error if the named
        # variable already exists.
        var_name = 'projects/{0}/configs/foo/variables/var1'.format(
            self.Project())
        request = self.messages.Variable(
            name=var_name,
            value=b'value1',
        )
        wanted_result = self.messages.Variable(
            name=var_name,
            state=self.messages.Variable.StateValueValuesEnum.UPDATED,
            updateTime='2016-04-16T00:00:00Z',
            value=b'value1',
        )

        self.variable_client.Update.Expect(request, wanted_result)
        got_result = self.RunRuntimeConfig(
            'variables set var1 "value1" --config-name foo '
            '--fail-if-absent')

        self.assertEqual(util.FormatVariable(wanted_result), got_result)
예제 #13
0
    def testSetMultiSegmentName(self):
        config_name = 'projects/{0}/configs/foo'.format(self.Project())
        var_name = 'projects/{0}/configs/foo/variables/var1/var2'.format(
            self.Project())
        request = self.messages.RuntimeconfigProjectsConfigsVariablesCreateRequest(
            parent=config_name,
            variable=self.messages.Variable(
                name=var_name,
                value=b'value1',
            ),
        )
        wanted_result = self.messages.Variable(
            name=var_name,
            updateTime='2016-04-16T00:00:00Z',
            value=b'value1',
        )

        self.variable_client.Create.Expect(request, wanted_result)
        got_result = self.RunRuntimeConfig(
            'variables set var1/var2 "value1" --config-name foo')

        self.assertEqual(util.FormatVariable(wanted_result), got_result)
예제 #14
0
    def testListReturnValues(self):
        request = self.messages.RuntimeconfigProjectsConfigsVariablesListRequest(
            parent='projects/{0}/configs/foo'.format(self.Project()),
            pageSize=self.DEFAULT_PAGE_SIZE,
            returnValues=True,
        )

        variables = [
            self.messages.Variable(
                name='projects/{0}/configs/foo/variables/var1'.format(
                    self.Project()),
                updateTime='2016-04-16T00:00:00Z',
                value=b'This is var1.',
            ),
            self.messages.Variable(
                name='projects/{0}/configs/foo/variables/var2/var3'.format(
                    self.Project()),
                updateTime='2016-04-16T01:00:00Z',
                value=b'This is var2/var3',
            ),
            self.messages.Variable(
                name='projects/{0}/configs/foo/variables/var4'.format(
                    self.Project()),
                updateTime='2016-04-16T02:00:00Z',
                text='This is var4',
            ),
        ]
        wrapped_result = self.messages.ListVariablesResponse(
            variables=variables, )

        self.variable_client.List.Expect(request, wrapped_result)
        got_result = self.RunRuntimeConfig(
            'variables list --config-name foo --values')

        self.assertEqual([util.FormatVariable(v, True) for v in variables],
                         list(got_result))
예제 #15
0
    def testSetFromStdIn(self):
        config_name = 'projects/{0}/configs/foo'.format(self.Project())
        var_name = 'projects/{0}/configs/foo/variables/var1'.format(
            self.Project())
        request = self.messages.RuntimeconfigProjectsConfigsVariablesCreateRequest(
            parent=config_name,
            variable=self.messages.Variable(
                name=var_name,
                value=b'line1\nline2\n',
            ),
        )
        wanted_result = self.messages.Variable(
            name=var_name,
            updateTime='2016-04-16T00:00:00Z',
            value=b'line1\nline2\n',
        )

        self.variable_client.Create.Expect(request, wanted_result)
        # WriteInput appends a '\n' to each line.
        self.WriteInput('line1', 'line2')
        got_result = self.RunRuntimeConfig(
            'variables set var1 --config-name foo')

        self.assertEqual(util.FormatVariable(wanted_result), got_result)
예제 #16
0
 def Run(self, args):
     result = super(Describe, self).Run(args)
     # Describe always returns the value.
     return util.FormatVariable(result, True)
예제 #17
0
 def Run(self, args):
     result = super(Describe, self).Run(args)
     return util.FormatVariable(result)