go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/lucicfg/starlark/stdlib/internal/luci/rules/task_backend.star (about)

     1  # Copyright 2022 The LUCI Authors.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #      http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  """TaskBackend integration settings."""
    16  
    17  load("@stdlib//internal/graph.star", "graph")
    18  load("@stdlib//internal/lucicfg.star", "lucicfg")
    19  load("@stdlib//internal/validate.star", "validate")
    20  load("@stdlib//internal/luci/common.star", "keys")
    21  
    22  def _validate_config(attr, val):
    23      """Valdiates that the value is in the specified backend config format required.
    24  
    25      attr: field name with this value, for error messages.
    26      val: a value to validate.
    27      required: if False, allow 'val' to be None, return 'default' in this case.
    28      """
    29      if not val:
    30          return None
    31      elif type(val) == "dict":
    32          return json.encode(validate.str_dict(attr, val))
    33      elif type(val).startswith("proto.Message"):
    34          return proto.to_jsonpb(val)
    35      else:
    36          fail("bad config: config needs to be a dict with string keys or a proto message.")
    37  
    38  def _validate_target(attr, val, required = True):
    39      """Valdiates that the value is the specified backend target format required.
    40  
    41      e.g. swarming://chromium-swarm
    42  
    43      attr: field name with this value, for error messages.
    44      val: a value to validate.
    45      required: if False, allow 'val' to be None, return 'default' in this case.
    46      """
    47      val = validate.string(attr = attr, val = val, required = required)
    48  
    49      invalid_keywords = ["http", "rpc"]
    50      for word in invalid_keywords:
    51          if word in val:
    52              fail("bad %r: found invaid word: %s in value: %s", (attr, word, val))
    53      if len(val.split("://")) != 2:
    54          fail("bad %r: invalid format for %s" % (attr, val))
    55      return val
    56  
    57  def _task_backend(
    58          ctx,  # @unused
    59          name = None,
    60          target = None,
    61          config = None):
    62      """Specifies how Buildbucket should integrate with TaskBackend.
    63  
    64      Args:
    65        ctx: the implicit rule context, see lucicfg.rule(...).
    66        name: A local name of the task backend. Required.
    67        target: URI for this backend, e.g. "swarming://chromium-swarm". Required.
    68        config: A dict with string keys or a proto message to be interpreted as
    69          JSON encapsulating configuration for this backend.
    70      """
    71      key = keys.task_backend(name)
    72      graph.add_node(key, props = {
    73          "target": _validate_target("target", target),
    74          "config": _validate_config("config", config),
    75      })
    76      return graph.keyset(key)
    77  
    78  task_backend = lucicfg.rule(impl = _task_backend)