[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/drydock/management/ -> DrydockManagementCreateResourceWorkflow.php (source)

   1  <?php
   2  
   3  final class DrydockManagementCreateResourceWorkflow
   4    extends DrydockManagementWorkflow {
   5  
   6    public function didConstruct() {
   7      $this
   8        ->setName('create-resource')
   9        ->setSynopsis('Create a resource manually.')
  10        ->setArguments(
  11          array(
  12            array(
  13              'name'      => 'name',
  14              'param'     => 'resource_name',
  15              'help'      => 'Resource name.',
  16            ),
  17            array(
  18              'name'      => 'blueprint',
  19              'param'     => 'blueprint_id',
  20              'help'      => 'Blueprint ID.',
  21            ),
  22            array(
  23              'name'      => 'attributes',
  24              'param'     => 'name=value,...',
  25              'help'      => 'Resource attributes.',
  26            ),
  27          ));
  28    }
  29  
  30    public function execute(PhutilArgumentParser $args) {
  31      $console = PhutilConsole::getConsole();
  32  
  33      $resource_name = $args->getArg('name');
  34      if (!$resource_name) {
  35        throw new PhutilArgumentUsageException(
  36          'Specify a resource name with `--name`.');
  37      }
  38  
  39      $blueprint_id = $args->getArg('blueprint');
  40      if (!$blueprint_id) {
  41        throw new PhutilArgumentUsageException(
  42          'Specify a blueprint ID with `--blueprint`.');
  43      }
  44  
  45      $attributes = $args->getArg('attributes');
  46      if ($attributes) {
  47        $options = new PhutilSimpleOptions();
  48        $options->setCaseSensitive(true);
  49        $attributes = $options->parse($attributes);
  50      }
  51  
  52      $viewer = $this->getViewer();
  53  
  54      $blueprint = id(new DrydockBlueprintQuery())
  55        ->setViewer($viewer)
  56        ->withIDs(array($blueprint_id))
  57        ->executeOne();
  58      if (!$blueprint) {
  59        throw new PhutilArgumentUsageException(
  60          'Specified blueprint does not exist.');
  61      }
  62  
  63      $resource = id(new DrydockResource())
  64        ->setBlueprintPHID($blueprint->getPHID())
  65        ->setType($blueprint->getImplementation()->getType())
  66        ->setName($resource_name)
  67        ->setStatus(DrydockResourceStatus::STATUS_OPEN);
  68      if ($attributes) {
  69        $resource->setAttributes($attributes);
  70      }
  71      $resource->save();
  72  
  73      $console->writeOut("Created Resource %s\n", $resource->getID());
  74      return 0;
  75    }
  76  
  77  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1