/** * @param int $ownerId * @param string|int $ownerType * @param array $fill Any extra attributes to update * * @return bool|AppKey False if owner is not authorized or on error, otherwise the created AppKey model is returned */ public static function createKey($ownerId, $ownerType, $fill = []) { $_owner = OwnerTypes::getOwner($ownerId, $ownerType); return static::_makeKey($_owner->id, $ownerType, AppKeyClasses::fromOwnerType($ownerType), $fill); }
/** * @param int $ownerId * @param int|string $ownerType String types will be converted to numeric equivalent * * @return EnterpriseModel|Cluster|User|Instance|Server|\stdClass */ public static function getOwner($ownerId, &$ownerType) { $_message = 'The owner id "' . $ownerType . ':' . $ownerId . '" could not be found.'; if (!is_numeric($ownerType)) { try { $ownerType = OwnerTypes::defines(strtoupper($ownerType), true); } catch (\InvalidArgumentException $_ex) { // Force a FAIL $ownerId = $ownerType = -1; } } // Owner types >= 1000 are reserved for logical, non-physical, entities such as the console or dashboard if ($ownerType >= static::CONSOLE) { $_owner = new \stdClass(); $_owner->id = $ownerId; $_owner->type = $ownerType; return $_owner; } // And the rest have models // @todo make more dynamic so new constants don't require new lookup switch cases switch ($ownerType) { case static::USER: return static::_lookupUser($ownerId); case static::SERVICE_USER: return static::_lookupServiceUser($ownerId); case static::MOUNT: return static::_lookupMount($ownerId); case static::INSTANCE: return static::_lookupInstance($ownerId); case static::SERVER: return static::_lookupServer($ownerId); case static::CLUSTER: return static::_lookupCluster($ownerId); } throw new ModelNotFoundException($_message); }
/** * @return string */ public function getOwnerTypeName() { return OwnerTypes::prettyNameOf($this->ownerType); }
/** * @param string $entityType Given an entity type, return the associated owner type * * @return bool */ public static function mapOwnerType($entityType) { return OwnerTypes::defines(strtoupper(trim($entityType)), true); }
/** * @param int $id * @param int $type * * @return \DreamFactory\Enterprise\Database\Models\Cluster|\DreamFactory\Enterprise\Database\Models\Instance|\DreamFactory\Enterprise\Database\Models\Server|\DreamFactory\Enterprise\Database\Models\User */ protected static function findOwner($id, $type = OwnerTypes::USER) { try { $_owner = OwnerTypes::getOwner($id, $type); } catch (\Exception $_ex) { is_string($id) && ($_owner = User::byEmail($id)->first()); } finally { if (empty($_owner)) { throw new ModelNotFoundException('The owner-id "' . $id . '" could not be found.'); } } return $_owner; }
/** * Create a new instance record * * @param string $instanceName * @param array $options Array of options for creation. Options are: * * owner-id The id of the instance owner * cluster-id The cluster that owns this instance * trial If true, the "trial" flagged is set for the instance * * @return Instance * @throws DuplicateInstanceException * @throws ProvisioningException */ public function make($instanceName, $options = []) { try { // Basic checks... if (null === ($_ownerId = array_get($options, 'owner-id'))) { throw new \InvalidArgumentException('No "owner-id" given. Cannot create instance.'); } if (null == ($_ownerType = array_get($options, 'owner-type'))) { $_ownerType = OwnerTypes::USER; } try { $_owner = OwnerTypes::getOwner($_ownerId, $_ownerType); } catch (ModelNotFoundException $_ex) { throw new \InvalidArgumentException('The "owner-id" and/or "owner-type" specified is/are invalid.'); } //$this->debug('owner validated: ' . $_owner->id . ($_owner->admin_ind ? ' (admin)' : ' (non-admin)')); if (false === ($_sanitized = Instance::isNameAvailable($instanceName, $_owner->admin_ind))) { throw new DuplicateInstanceException('The instance name "' . $instanceName . '" is not available.'); } // Get the proper location $_guestLocation = array_get($options, 'guest-location', config('provisioning.default-guest-location')); // Validate the cluster and pull component ids $_clusterId = array_get($options, 'cluster-id', config('provisioning.default-cluster-id')); $_clusterConfig = $this->getServersForCluster($_clusterId); $_ownerId = $_owner->id; $_attributes = ['user_id' => (int) $_ownerId, 'instance_id_text' => $_sanitized, 'instance_name_text' => $_sanitized, 'guest_location_nbr' => $_guestLocation, 'cluster_id' => (int) $_clusterConfig['cluster-id'], 'db_server_id' => (int) $_clusterConfig['db-server-id'], 'app_server_id' => (int) $_clusterConfig['app-server-id'], 'web_server_id' => (int) $_clusterConfig['web-server-id'], 'state_nbr' => ProvisionStates::CREATED]; $_guestAttributes = ['instance_id' => null, 'vendor_id' => $_guestLocation, 'vendor_image_id' => array_get($options, 'vendor-image-id', config('provisioning.default-vendor-image-id')), 'vendor_credentials_id' => array_get($options, 'vendor-credentials-id', config('provisioning.default-vendor-credentials-id'))]; // Write it out return \DB::transaction(function () use($_ownerId, $_attributes, $_guestAttributes) { $_instance = Instance::create($_attributes); //$this->debug('created instance row id#' . $_instance->id); $_guestAttributes['instance_id'] = $_instance->id; $_guest = InstanceGuest::create($_guestAttributes); //$this->debug('created guest row id#' . $_guest->id); if (!$_instance || !$_guest) { throw new \RuntimeException('Instance creation failed'); } return $_instance; }); } catch (\Exception $_ex) { throw new ProvisioningException('Error creating new instance: ' . $_ex->getMessage()); } }
/** @inheritdoc */ protected function getOptions() { return array_merge(parent::getOptions(), [['mount-type', 't', InputOption::VALUE_REQUIRED, 'The type of mount: ' . implode(', ', MountTypes::getDefinedConstants(true))], ['owner-id', null, InputOption::VALUE_REQUIRED, 'The "owner-id" of this mount'], ['owner-type', null, InputOption::VALUE_REQUIRED, 'The type of owner: ' . implode(', ', OwnerTypes::getDefinedConstants(true))], ['root-path', 'p', InputOption::VALUE_REQUIRED, 'The "root-path" of the mount'], ['config', 'c', InputOption::VALUE_REQUIRED, 'JSON-encoded array of configuration data for this mount']]); }
/** @inheritdoc */ protected function getOptions() { return array_merge(parent::getOptions(), [['owner-id', null, InputOption::VALUE_REQUIRED, 'The "owner-id" of this cluster'], ['owner-type', null, InputOption::VALUE_REQUIRED, 'The type of owner: ' . implode(', ', OwnerTypes::getDefinedConstants(true))], ['subdomain', null, InputOption::VALUE_REQUIRED, 'The subdomain in which this cluster resides'], ['max-instances', 'm', InputOption::VALUE_REQUIRED, 'The maximum number of instances allowed, if any.'], ['server-id', null, InputOption::VALUE_REQUIRED, 'The "server-id" to "add" or "remove"']]); }
/** * Get the console command arguments. * * @return array */ protected function getArguments() { return array_merge(parent::getArguments(), [['owner-id', InputArgument::REQUIRED, 'The id of the owner of this key'], ['owner-type', InputArgument::REQUIRED, 'One of the following owner types: ' . OwnerTypes::prettyList()]]); }