MscmpSystInstance (mscmp_syst_instance v0.1.0)

API allowing for the management of application Instances.

"Instances" are instances of running application environments. Instances are established to host the application for different purposes, such as for running the application for production, training, and testing purposes; or as a means to implement multi-tenancy where each tenant application environment is an Instance.

Each Instance also requires supporting data in order to facilitate runtime actions, such as defining database roles with which to access database data. Such supporting information is also managed via this component.

Concepts

Instances are organized into a framework of other data types which allows us to better analyze and manage them and provides supplemental information required for Instance operation.

Datastores & Datastore Contexts

Each Instance is associated with a single Datastore. A Datastore is the database created to store the Instance's application data. Datastore Contexts are the database roles with which the Instance will access the database; this allows for application database connections secured to an appropriate range of actions and limited to an appropriate number of concurrent database connections. Or to put it another way, Datastore Contexts can be thought of as "Database Access Contexts".

Datastore and Datastore Context management is provided by the MscmpSystDb component.

Owners

Owners represent the known tenants of the system. An Owner will own Instances of Applications. Owners have states which determine if they are active, not active, or even if they may be purged from the system.

Applications

Each Instance is an operational instance of a specific Application. An Application is simply what an end user would understand to be application: computer software which solves some problem for them better than could be solved without such software.

Applications in this component are added and maintained directly via database migrations therefore there is no API provided to manage applications and there are no user maintainable Application attributes.

Application Contexts

Application Context records define the expected Datastore Context records that must be created for each Instance. Each Application Context will be represent data ownership, data access restriction, and/or connection pool resource constraint. When created Instances will have its own database roles created for each of the defined Application Contexts.

Instance Types

Instance Type records are used to define the default resource utilization classes that are available for creating new Instances. These defaults include such starting values as the number of database connections to use for each Application Context and which server pools may host the Instance.

After Instance creation, the Instance Type serves no further role for the Instance.

Note that Instance Type records are a special case of the Msdata.SystEnumItems record. The Enum Name for Instance Type is, unsurprisingly, instance_types.

Instance Type Applications

Establishes that a given Instance Type will support a particular Application.

Instance Type Contexts

Defines defaults used in creating Instance Context records at Instance creation time.

When an association is made between an Instance Type and an Application via a Instance Type Application association, a new Instance Type Context record is created for the combination of the Instance Type and each of the Application's defined Application Contexts.

At Instance creation time, the Instance Type Context defaults are copied into Instance specific Instance Context records to define the actual database roles which the instance will use to interact with the database.

Note that Instance Type Context records are automatically created and destroyed via the creation of Instance Type Application records by way of database triggers; therefore the only application API related to these records is for updating them.

Instances

An Instance represents the single environment of a specific Application and a specific Owner. Instance records are used to know which database server to connect to, what Instance Context records are used for connecting to the database, and the current state of the Instance including whether or not it's been updated with the Application database definition, is in a usable state, or can be purged from the system.

Instance Contexts

An Instance's Instance Context records define specifically how an Instance will connect to its Datastore including such information as how many connections should be opened to the database.

Database triggers are responsible for creating and destroying Instance Context records. On creation, the Instance Context is created with default values defined by Instance Type Context records for the type of Instance being created. The default values set on Instance Context creation may be overridden via this API as desired.

Special Notes

This component requires that an instance of the MscmpSystEnums service has been configured and started. Many of the API calls in this component will fail if the MscmpSystEnums services are not available.

Summary

Owners

Creates a new Owner record.

Retrieves an Owner record by its internal name.

Retrieves the Owner record ID by Owner internal name.

Returns the Owner State Enumeration record for the given Internal Name argument.

Returns the Owner State Enumeration record which is configured as being default.

Tests to see if a specific Owner, or any Owner, record exists in the database.

Removes an Owner record from the system.

Updates an Owner record.

Instance Types

Creates a new Instance Type record via the MscmpSystEnums service.

Creates an Instance Type/Application association.

Deletes an Instance Type from the MscmpSystEnums service.

Disassociates the Instance Type/Application support relationship.

Returns the Instance Type record for the given Internal Name; raises on error.

Returns the Instance Type record which is configured as the system default Instance Type.

Updates an existing Instance Type record via the MscmpSystEnums service.

Updates Instance Type Context default values which are applied to new Instance Contexts.

Instances

Creates a new Instance record.

Returns a Keyword List of the default values for each Instance State's functional type.

Returns the SystInstances record identified by its internal name.

Retrieves the Datastore Options based on the Instance database record and the provided Startup Options.

Returns the ID of the Instance record as identified by its internal name.

Retrieves the Instance State Enumeration record identified by the provided Internal Name.

Returns the Instance State Enumeration record which is configured as being default.

Initializes an uninitialized Instance.

Purges an eligible Instance from the system.

Sets the Instance State of an Instance.

Applications

Creates a new Application record.

Creates Application Context records for the identified Application.

Deletes an Application Context record from the system

Returns a populated Msdata.SystApplications struct for the requested record.

Retrieves the Application Context record ID for the record matching provided Internal Name argument.

Returns the Application record ID for the requested Application Internal Name; raises on error.

Returns a list of Application Context records.

Updates an existing Application record using the provided parameters as new values.

Updates an existing Application Context record.

Runtime

Starts all Applications and each Application's child Instances.

Starts the requested Application and its child Instances.

Starts an Instance and places it under the appropriate Application's supervisor.

Starts the MscmpSystInstance services and supervisors.

Stops all Applications, Application Supervisors, and their child Instances.

Shuts down the Supervisor process of the requested Application and all of its child processes, including Instances.

Owners

Link to this function

create_owner(owner_params)

@spec create_owner(MscmpSystInstance.Types.owner_params()) ::
  {:ok, Msdata.SystOwners.t()} | {:error, MscmpSystError.t()}

Creates a new Owner record.

Parameters

  • owner_params - a map of Owner record attributes to use when creating a new record.

    • internal_name - a predetermined unique identifier for the Owner record for use in programmatic contexts. This attribute is required and must be unique in the system.

    • display_name - a unique, friendly name identifying the Owner and for use in user interfaces. This attribute is required and must be unique in the system.

    • owner_state_id - the Owner State value with which to create the new Owner record. If not provided in the function call, this attribute will be defaulted to the configured Owner State default value for the enumeration.

Link to this function

get_owner_by_name(owner_name)

@spec get_owner_by_name(MscmpSystInstance.Types.owner_name()) ::
  {:ok, Msdata.SystOwners.t()} | {:error, MscmpSystError.t()}

Retrieves an Owner record by its internal name.

This retrieval operation will also populate the associated records of Owner State and the Owner State Functional Type.

Examples

iex> {:ok, %Msdata.SystOwners{internal_name: "owner1"}} = ...> MscmpSystInstance.get_owner_by_name("owner1")

Link to this function

get_owner_id_by_name(owner_name)

@spec get_owner_id_by_name(MscmpSystInstance.Types.owner_name()) ::
  {:ok, MscmpSystInstance.Types.owner_id()} | {:error, MscmpSystError.t()}

Retrieves the Owner record ID by Owner internal name.

Examples

iex> {:ok, owner_id} = MscmpSystInstance.get_owner_id_by_name("owner1") iex> is_binary(owner_id) true

Link to this function

get_owner_state_by_name(owner_state_name)

@spec get_owner_state_by_name(MscmpSystInstance.Types.owner_state_name()) ::
  Msdata.SystEnumItems.t() | nil

Returns the Owner State Enumeration record for the given Internal Name argument.

If the requested Internal Name does not match an existing Owner State Enumeration record nil is returned.

Parameters

  • owner_state_name - the internal name of the Owner State to retrieve.

Examples

Retrieving an Owner State Enumeration record.

iex> %Msdata.SystEnumItems{internal_name: "owner_states_sysdef_active"} =
...>   MscmpSystInstance.get_owner_state_by_name("owner_states_sysdef_active")

Trying to retrieve a non-existent Owner State.

iex> MscmpSystInstance.get_owner_state_by_name("nonexistent_state")
nil
Link to this function

get_owner_state_default(functional_type \\ nil)

Returns the Owner State Enumeration record which is configured as being default.

If no Owner State record is configured as default, then nil is returned.

Parameters

  • functional_type - an optional parameter which, if provided and not nil, will return the default Owner State record configured for the requested functional type rather than the system default Owner State. The default for this parameter is to treat the parameter as not provided (nil).

Examples

Requesting the system default Owner State.

iex> %Msdata.SystEnumItems{internal_name: "owner_states_sysdef_active"} =
...>   MscmpSystInstance.get_owner_state_default()

Requesting the default Owner State for a specific functional type.

iex> %Msdata.SystEnumItems{internal_name: "owner_states_sysdef_inactive"} =
...>   MscmpSystInstance.get_owner_state_default(:owner_states_inactive)
Link to this function

owner_exists?(opts \\ [])

@spec owner_exists?(Keyword.t()) :: boolean() | {:error, MscmpSystError.t()}

Tests to see if a specific Owner, or any Owner, record exists in the database.

The functions provides an optional test on either an Owner record's Internal Name or record ID value. If no selectivity option is made, the test checks if any Owner records exist in the database at all.

If the Owner record(s) is found, the function returns true; other false. Errors produce a result tuple.

Parameters

  • opts - an optional Keyword List of optional parameters which can influence the result of calling the function. The available options are:

    • owner_id - tests if a specific Owner record exists as referenced by its record ID value.

    • owner_name - tests if a specific Owner record exists as referenced by its Internal Name.

Examples

Check if any Owner record exists.

iex> MscmpSystInstance.owner_exists?()
true

Check if a specific Owner record exists.

iex> MscmpSystInstance.owner_exists?(owner_name: "owner1")
true

If a non-existent Owner is requested, the function indicates the record was not found.

iex> MscmpSystInstance.owner_exists?(owner_name: "nonexistent_owner")
false
Link to this function

purge_owner(owner)

@spec purge_owner(MscmpSystInstance.Types.owner_id() | Msdata.SystOwners.t()) ::
  :ok | {:error, MscmpSystError.t()}

Removes an Owner record from the system.

Note that only Owner records in an Owner State of functional type owner_states_purge_eligible may be purged from the system.

Parameters

  • owner - either the record ID of an Owner record to delete or the Msdata.SystOwners struct representing the Owner to purge.
Link to this function

update_owner(owner, update_params)

Updates an Owner record.

Parameters

  • owner - This value must either by the record ID of an existing Owner record or the Msdata.SystOwners struct representing an existing owner.

  • update_params - a map of Owner record attributes to be updated. For update operations only those attributes to be updates must be provided.

    • internal_name - a predetermined unique identifier for the Owner record for use in programmatic contexts. This attribute must be unique in the system and may not be nil.

    • display_name - a unique, friendly name identifying the owner and for use in user interfaces. This attribute must be unique in the system and may not be nil.

    • owner_state_id - the record ID value of the new Owner State of the Owner record. Note that if this attribute is provided, but is set nil, an error will result.

Instance Types

Link to this function

create_instance_type(instance_type_params)

@spec create_instance_type(MscmpSystInstance.Types.instance_type_params()) ::
  {:ok, Msdata.SystEnumItems.t()} | {:error, MscmpSystError.t()}

Creates a new Instance Type record via the MscmpSystEnums service.

Parameters

  • instance_type_params - the parameters to use when creating the new Instance Type. The attributes internal_name, display_name, external_name, user_description, and user_options fields are required for Instance Type creation.
Link to this function

create_instance_type_application(instance_type_id, application_id)

Creates an Instance Type/Application association.

Associating an Instance Type with an Application expresses that the Instance Type can support Instances of the given application.

Parameters

  • instance_type_id - the ID value of the Instance Type which will support Application Instances.

  • application_id - the ID value which identifies the Application to be supported.

Link to this function

delete_instance_type(instance_type_name)

@spec delete_instance_type(MscmpSystInstance.Types.instance_type_name()) ::
  :ok | {:error, MscmpSystError.t()}

Deletes an Instance Type from the MscmpSystEnums service.

Note that an attempt to delete an in-use Instance Type will result in a constraint error.

Parameters

  • instance_type_name - the internal name of an Instance Type to delete from the system.
Link to this function

delete_instance_type_application(instance_type_application)

@spec delete_instance_type_application(
  MscmpSystInstance.Types.instance_type_application_id()
  | Msdata.SystInstanceTypeApplications.t()
) :: :ok | {:error, MscmpSystError.t()}

Disassociates the Instance Type/Application support relationship.

Note that this will only prevent the creation of new Instances of the Instance Type for the Application. Existing Instances that were created when the relationship was valid will continue to exist.

Parameters

  • instance_type_application - This value can be either the ID value of the Instance Type Application record or a Msdata.SystInstanceTypeApplications struct with at least the id field defined.
Link to this function

get_instance_type_by_name(instance_type_name)

@spec get_instance_type_by_name(MscmpSystInstance.Types.instance_type_name()) ::
  Msdata.SystEnumItems.t() | nil

Returns the Instance Type record for the given Internal Name; raises on error.

On successful execution either the requested Instance Type Enumeration record is returned or nil if the record does not exist.

Parameters

  • instance_type_name - the Internal Name of the desire Instance Type record to return.

Examples

Finding a Instance Type record by Internal Name.

iex> %Msdata.SystEnumItems{} =
...>   MscmpSystInstance.get_instance_type_by_name("instance_types_big")

Looking for a non-existent record.

iex> MscmpSystInstance.get_instance_type_by_name("nonexistent_type")
nil
Link to this function

get_instance_type_default()

@spec get_instance_type_default() :: Msdata.SystEnumItems.t()

Returns the Instance Type record which is configured as the system default Instance Type.

If no system default has not been defined nil is returned.

Examples

iex> %Msdata.SystEnumItems{} = MscmpSystInstance.get_instance_type_default()
Link to this function

update_instance_type(instance_type_name, instance_type_params \\ %{})

Updates an existing Instance Type record via the MscmpSystEnums service.

Parameters

  • instance_type_name - the internal name of the Instance Type to target for updating.

  • instance_type_params - the parameters to use when creating the new Instance Type. All attributes in the parameter map are optional in updating contexts.

Link to this function

update_instance_type_context(instance_type_context, instance_type_context_params \\ %{})

Updates Instance Type Context default values which are applied to new Instance Contexts.

Parameters

  • instance_type_context - this value may either be the record ID of the target Instance Type Context record or a copy of the current Msdata.SystInstanceTypeContexts struct representing the target record.

  • instance_type_context_params - a map containing those attributes to be updated with their new values.

    • instance_type_id - the ID value of the owning Instance Type record. This value is required unless the instance_type_name attribute is provided.

    • instance_type_name - the internal name value of the owning Instance Type record. This value required unless the instance_type_id value is provided instead.

    • application_context_id - the record ID value of the Application Context with which this record is associated. This value is required unless the application_context_name value is provided instead.

    • application_context_name - the internal name value of the Application Context with which this record is associated. This value is required unless the application_context_id value is provided instead.

    • default_db_pool_size - the default size of the database connection pool to use for Instances Contexts created for Instances of this Instance Type. This field defaults to 0 on Instance Type Context creation.

Instances

Link to this function

create_instance(instance_params)

@spec create_instance(MscmpSystInstance.Types.instance_params()) ::
  {:ok, Msdata.SystInstances.t()} | {:error, MscmpSystError.t()}

Creates a new Instance record.

Creating a new Instance record will also create new Instance Context records based on the defaults defined in the Instance Type Context records associated with the Instance Type and Application of the new Instance. This is accomplished with database triggers.

Parameters

  • instance_params - A map of attributes to use in creating the new Instance record in the system.

    • internal_name - a pre-generated identifier for Instance record for use in programmatic contexts. This value is required and must be unique in the system.

    • display_name - a friendly name which identifies the record for use in user interfaces. This value is required and must be unique in the system.

    • dbserver_name - the name of the database server where the Instance Datastore exists. This value is required. Note that invalid values here may not be detected until such time as the Instance Datastore use is attempted.

    • application_id - the record ID of the Application for which this Instance is being created. This value is required unless the application_name attribute has been provided instead.

    • application_name - the internal name of the Application for which this Instance is being created. This value is requires unless the application_id attribute has been provided instead.

    • instance_code - A value used in constructing the Instance Context credentials. This value should be a randomly generated series of between 8 and 64 bytes.

    • instance_type_id - the record ID of the Instance Type of the new Instance record. This attribute is required unless the instance_type_name attribute is provided instead.

    • instance_type_name - the internal name of the Instance Type of the new Instance record. This attribute is requires unless the instance_type_id attribute is provided instead.

    • instance_state_id - the record ID of the Instance State in which to create the new Instance record. This value may be omitted and allowed to default based on the configured default Instance State. Optionally the Instance State may also be identified by the instance_state_name attribute.

    • instance_state_name - the internal name of the Instance State in which to create the new Instance record. This value may be omitted and allowed to default based on the configured default Instance State. Optionally the Instance State may also be identified by the instance_state_id attribute.

    • owner_id - the record ID of the Owner of the Instance. This attribute is required unless the owner_name attribute is provided.

    • owner_name - th internal name of the Owner of the Instance. This attribute is required unless the owner_id attribute is provided.

    • owning_instance_id - if the Instance is associated with a parent instance, such as a sandbox Instance being created for a parent production Instance, the parent/child relationship may be expressed by assigning this attribute to the record ID of the parent. This attribute is not required and the parent Instance may be identified by the owning_instance_name attribute instead.

    • owning_instance_name - an alternate identification method for identifying a parent Instance when creating a child Instance. This attribute is optional and may used in lieu of using the owning_instance_id attribute to establish the parent/child relationship of the new Instance.

Link to this function

get_default_instance_state_ids()

@spec get_default_instance_state_ids() :: Keyword.t()

Returns a Keyword List of the default values for each Instance State's functional type.

Instance updating functions that change the Instance State value of the Instance will default to the appropriate Instance State if a specific Instance State value is not provided by the caller.

Link to this function

get_instance_by_name(instance_name)

@spec get_instance_by_name(MscmpSystInstance.Types.instance_name()) ::
  {:ok, Msdata.SystInstances.t()} | {:error, MscmpSystError.t()}

Returns the SystInstances record identified by its internal name.

Parameters

  • instance_name - the internal name of the Instance record to return.

Example

iex> {:ok, %Msdata.SystInstances{}} =
...>   MscmpSystInstance.get_instance_by_name("app1_owner1_instance_types_sml")
Link to this function

get_instance_datastore_options(instance, startup_options)

Retrieves the Datastore Options based on the Instance database record and the provided Startup Options.

Parameters

  • instance - the instance parameter is either the record ID value of the Instance record desired or the Msdata.SystInstances struct for the target Instance.

  • startup_options - a map of values containing the Startup Options obtained from the MscmpSystOptions component.

Link to this function

get_instance_id_by_name(instance_name)

@spec get_instance_id_by_name(MscmpSystInstance.Types.instance_name()) ::
  {:ok, MscmpSystInstance.Types.instance_id()} | {:error, MscmpSystError.t()}

Returns the ID of the Instance record as identified by its internal name.

Parameters

  • instance_name - the internal name of the Instance record to reference.

Example

iex> {:ok, instance_id} =
...>   MscmpSystInstance.get_instance_id_by_name("app1_owner1_instance_types_sml")
iex> is_binary(instance_id)
true
Link to this function

get_instance_state_by_name(instance_state_name)

@spec get_instance_state_by_name(MscmpSystInstance.Types.instance_state_name()) ::
  Msdata.SystEnumItems.t() | nil

Retrieves the Instance State Enumeration record identified by the provided Internal Name.

If the requested Internal Name does not match an existing Instance State Enumeration record nil is returned.

Parameters

  • instance_state_name - the internal name of the Instance State to retrieve.

Examples

Retrieving an Instance State Enumeration record.

iex> %Msdata.SystEnumItems{internal_name: "instance_states_sysdef_active"} =
...>   MscmpSystInstance.get_instance_state_by_name("instance_states_sysdef_active")

Trying to retrieve a non-existent Instance State.

iex> MscmpSystInstance.get_instance_state_by_name("nonexistent_state")
nil
Link to this function

get_instance_state_default(functional_type \\ nil)

@spec get_instance_state_default(
  MscmpSystInstance.Types.instance_state_functional_types()
  | nil
) ::
  Msdata.SystEnumItems.t()

Returns the Instance State Enumeration record which is configured as being default.

If no Instance State record is configured as default, then nil is returned.

Parameters

  • functional_type - an optional parameter which, if provided and not nil, will return the default Instance State record configured for the requested functional type rather than the system default Instance State. The default for this parameter is to treat the parameter as not provided (nil).

Examples

Requesting the system default Instance State.

iex> %Msdata.SystEnumItems{internal_name: "instance_states_sysdef_uninitialized"} =
...>   MscmpSystInstance.get_instance_state_default()

Requesting the default Instance State for a specific functional type.

iex> %Msdata.SystEnumItems{internal_name: "instance_states_sysdef_active"} =
...>   MscmpSystInstance.get_instance_state_default(:instance_states_active)
Link to this function

initialize_instance(instance_id, startup_options, opts \\ [])

@spec initialize_instance(MscmpSystInstance.Types.instance_id(), map(), Keyword.t()) ::
  {:ok, Msdata.SystInstances.t()} | {:error, MscmpSystError.t()}

Initializes an uninitialized Instance.

When a new Instance is created in the database, the record is giving an Instance State with a functional type of instance_states_uninitialized meaning that the Instance record has been created in the database, but the Instance's own database and associated database roles have not yet been set up.

Initializing an Instance creates its database and its database roles ("Datastore" and "Datastore Contexts"). Once initialized, the Instance record is given an Instance State of functional type instance_states_initialized.

Initialized Instances may be started and have their Datastores migrated to the current version of the Instance's database.

Parameters

  • instance_id - the record ID of the Instance to initialize.

  • startup_options - a map of values containing the Startup Options obtained from the MscmpSystOptions component.

  • opts - a Keyword List of optional values used during the initialization process. The available options include:

    • initializing_state_id - the record ID value of the Instance State to use to indicate that the record is being initialized. If this value is not provided the configured default Instance State for the functional type instance_states_initializing will be used.

    • initialized_state_id - the record ID value of the Instance State to use to indicate that the record has successfully been initialized. If this value is not provided the configured default Instance State for the functional type instance_states_initialized will be used.

    • failed_state_id - the record ID value of the Instance State to use to indicate that the record has failed to be initialized. If this value is not provided the default configured Instance State for the functional type instance_states_failure will be used.

    • db_shutdown_timeout - a timeout value used to limit the time allowed for a clean shutdown of the DBA and Privileged database connections used for initialization of the Datastore. See the documentation for MscmpSystDb.create_datastore/2 for more information.

Link to this function

purge_instance(instance, startup_options)

@spec purge_instance(
  MscmpSystInstance.Types.instance_id() | Msdata.SystInstances.t(),
  map()
) ::
  :ok | {:error, MscmpSystError.t()}

Purges an eligible Instance from the system.

Purging an Instance drops its associated Datastore and its defining Msdata.SystInstances records from the database.

In order for the purge to be successful, the Instance must be in an Instance State of functional type instance_states_purge_eligible.

Warning

Naturally, great care must be taken in the lead up to calling this function as purging an Instance leads to the irreversible loss of the Instance's data (absent externally managed backups of some sort).

Parameters

  • instance - either the record ID or the Msdata.SystInstances struct of the Instance to purge.

  • startup_options - a map of values containing the Startup Options obtained from the MscmpSystOptions component.

Link to this function

set_instance_state(instance, instance_state_id)

Sets the Instance State of an Instance.

Parameters

  • instance - the current Msdata.SystInstances struct representing the Instance to be updated.

  • instance_state_id - The record ID of the new Instance State value into which to place the Instance record.

Applications

Link to this function

create_application(application_params)

@spec create_application(MscmpSystInstance.Types.application_params()) ::
  {:ok, Msdata.SystApplications.t()} | {:error, MscmpSystError.t()}

Creates a new Application record.

Application Subsystems need means by which they can make the MscmpSystInstance component aware of their existence and this function provides the means by which to do that.

Note

Note that this function is meant to expose Application record management to the relevant Application Subsystem programs and is not intended for regular management activities by end users.

Parameters

  • application_params - the parameters with which the new Application record should be created. The following attribute values are available:

    • internal_name - a predetermined unique identifier for the Application record for use in programmatic contexts. This attribute is required and must be unique in the system.

    • display_name - a unique, friendly name identifying the Application and for use in user interfaces. This attribute is required and must be unique in the system.

    • syst_description - a user facing description of the Application including any special usage requirements or preconditions. This attribute is required.

Examples

Creating a new Application record.

iex> new_app_params = %{
...>   internal_name: "ex_app1",
...>   display_name: "Example App 1",
...>   syst_description: "An example application"
...> }
iex> {:ok, %Msdata.SystApplications{}} =
...>   MscmpSystInstance.create_application(new_app_params)
Link to this function

create_application_context(application_context_params)

@spec create_application_context(MscmpSystInstance.Types.application_context_params()) ::
  {:ok, Msdata.SystApplicationContexts.t()} | {:error, MscmpSystError.t()}

Creates Application Context records for the identified Application.

Application Contexts describe the Datastore Contexts each Instance is expected to support to allow an Application to access its data. Application Subsystems use this function to create the any required Application Context records not already registered in the MscmpSystInstance data. Application Contexts are used in the creation of Msdata.SystInstanceContexts records and provide a number of default values for the Instance Context records.

Note

Note that this function is meant to expose Application record management to the relevant Application Subsystem programs and is not intended for regular management activities by end users.

Parameters

  • application_context_params - a map defining the attributes which will be used to create the new Application Context record. The attributes are:

    • internal_name - a predetermined unique identifier for the Application Context record for use in programmatic contexts. This attribute is required and must be unique in the system.

    • display_name - a unique, friendly name identifying the Application Context and for use in user interfaces. This attribute is required and must be unique in the system.

    • application_id - a reference to the ID value of the parent Application record. A valid value for this attribute is required unless the application_name attribute is set with the Internal Name of an existing Application record. If both this attribute and the application_name attributes are set, the attribute_name value will be used to resolve the parent Application.

    • application_name - a reference to the Internal Name value of an existing Application record. This value is used to look-up the application_id and so if this attribute is provided with a valid value, the application_id attribute may be omitted. The value of this attribute takes precedence over any value set explicitly in the application_id attribute. If application_id is omitted, then this attribute is required.

    • description - a description of the Application Context's role in the application and database. This becomes a comment in the database attached to the database role created for the context.

    • start_context - a required boolean value which establishes the default value of derived Instance Context (Msdata.SystInstanceContexts) start_context settings. When true, an Instance Context record derived from this Application Context will be, by default, started as active database connections when the parent Instance is started. False indicates that by default Instance startup will not establish database connections for the context. This value muse be set false for any Application Context defining a Datastore Owner Context or any other Context where the login_context is set false.

    • login_context - a required boolean value which indicates if a derived Instance Context is used to create database connections. If true, a derived Instance Context record will provide login information to establish a database connection on Instance start so long as its start_context value is also true. If this attribute is set false the derived Instance Context record will not define a Context capable of logging into the database.

    • database_owner_context - a required boolean value which, when true, is designates an Application Context record as establishing the default values for Instance Datastore/database owners. If true, the start_context and login_context attributes must be set false as owner contexts are not used for database connectivity not may be started during the Instance start process.

Examples

Create an database owner Application Context record.

iex> new_context_params = %{
...>   internal_name: "ex_app2_owner",
...>   display_name: "Example App 2 Owner",
...>   application_name: "ex_app2",
...>   description: "Database role owning objects for 'ex_app2'.",
...>   start_context: false,
...>   login_context: false,
...>   database_owner_context: true
...> }
iex> {:ok, %Msdata.SystApplicationContexts{}} =
...>   MscmpSystInstance.create_application_context(new_context_params)
Link to this function

delete_application_context(application_context_id)

@spec delete_application_context(MscmpSystInstance.Types.application_context_id()) ::
  {:ok, :deleted | :not_found} | {:error, MscmpSystError.t()}

Deletes an Application Context record from the system

Application Subsystems may use this function to remove an obsolete Application Context record from the system.

Note

Note that this function is meant to expose Application record management to the relevant Application Subsystem programs and is not intended for regular management activities by end users.

Warning!

While this function will remove an Application Context record from the system and prevent any new Instances of the Application from including the deleted Context, existing Instance Contexts based on the Application Context are not currently cleaned up by this function. Any clean-up of existing Instance Context data and of associated database roles and verification of extended clean-up activities is therefore the responsibility of the caller.

On successful delete of the record, a success tuple is returned to the caller ({:ok, :deleted}). If the requested record is not found in the database, the returned value is {:ok, :not_found}. Any other outcome is returned via an error tuple.

Parameters

  • application_context_id - the record ID of the Application Context record to delete. This value is required.

Examples

Deleting an existing Application Context.

iex> record_id = MscmpSystInstance.get_application_context_id_by_name("ex_app2_delctx")
iex> MscmpSystInstance.delete_application_context(record_id)
{:ok, :deleted}

Attempting to delete a non-existent record.

iex> record_id = "00000000-0000-0000-0000-000000000000"
iex> MscmpSystInstance.delete_application_context(record_id)
{:ok, :not_found}
Link to this function

get_application(application, opts \\ [])

Returns a populated Msdata.SystApplications struct for the requested record.

Parameters

  • application - either the record ID of the desired Application record or its Internal Name. This parameter is required.

  • opts - allows optional parameters to be provided which govern the behavior of this function. The options are provided via a Keyword List. The available options are:

    • include_contexts - a boolean value indicating whether or not to also retrieve the fully populated list of :application_contexts associated with the requested Application. Contexts are returned as a standard association of Msdata.SystApplicationContexts structs. The default value of this option is false.
Link to this function

get_application_context_id_by_name(application_context_name)

Retrieves the Application Context record ID for the record matching provided Internal Name argument.

When the requested Application Context record can not be found this function returns nil. All errors raise an exception.

Parameters

  • application_context_name - the Internal Name value of the Application Context record to search for.

Examples

Finding an existing Application Context.

iex> id = MscmpSystInstance.get_application_context_id_by_name("ex_app2_idctx")
iex> is_binary(id)
true

Searching for a non-existent Application Context.

iex> MscmpSystInstance.get_application_context_id_by_name("nonexistent_context")
nil
Link to this function

get_application_id_by_name(application_name)

Returns the Application record ID for the requested Application Internal Name; raises on error.

On successful execution the record ID of the requested Application is returned. If the requested Application Internal Name is not found nil is returned.

Note

Note that this function is meant to expose Application record management to the relevant Application Subsystem programs and is not intended for regular management activities by end users.

Parameters

  • applicaton_name - the internal name of the desired Application record.

Examples

Finding an application returns its ID value.

iex> application_id = MscmpSystInstance.get_application_id_by_name("app1")
iex> is_binary(application_id)
true

Asking for a non-existent application returns nil.

iex>  MscmpSystInstance.get_application_id_by_name("nonexistent_application")
nil
Link to this function

list_application_contexts(application_id \\ nil)

@spec list_application_contexts(MscmpSystInstance.Types.application_id() | nil) ::
  {:ok, [Msdata.SystApplicationContexts.t()]} | {:error, MscmpSystError.t()}

Returns a list of Application Context records.

Parameters

  • application_id - an optional reference to a specific application for which to return Application Context records. By default this value is nil which results in all Application Context records for all Applications being returned.
Link to this function

update_application(application, application_params)

Updates an existing Application record using the provided parameters as new values.

Allows an Application Subsystem program to update its representative Application record as required.

Note

Note that this function is meant to expose Application record management to the relevant Application Subsystem programs and is not intended for regular management activities by end users.

Parameters

  • application - either a fully populated Msdata.SystApplications struct representing the current state of the Application record or the ID of the Application record to update. This argument is required.

  • application_params - a map containing the attributes with updated values for the Application record update operation. The attributes which may be updated are:

    • display_name - a unique, friendly name identifying the Application and for use in user interfaces. This value may not be set nil if it is included.

    • syst_description - a user facing description of the Application including any special usage requirements or preconditions. This attribute may not be set nil if it is included.

Examples

Creating a new Application record.

iex> target_app_id = MscmpSystInstance.get_application_id_by_name("ex_app2")
iex> update_app_params = %{
...>   display_name: "Example App #2",
...>   syst_description: "An updated example application."
...> }
iex> {:ok, %Msdata.SystApplications{display_name: "Example App #2"}} =
...>   MscmpSystInstance.update_application(target_app_id, update_app_params)
Link to this function

update_application_context(application_context, application_context_params)

Updates an existing Application Context record.

Allows an Application Subsystem to update its Application Context entries as permitted.

Note

Note that this function is meant to expose Application record management to the relevant Application Subsystem programs and is not intended for regular management activities by end users.

Parameters

  • application_context - this required parameter may either be the Application Context record ID or the fully populated Msdata.SystApplicationContexts struct to update.

  • application_context_params - a map of attributes which are to be updated with the new values of those attributes. The available attributes for updates are:

    • display_name - a unique, friendly name identifying the Application Context and for use in user interfaces. This attribute is required and must be unique in the system.

    • description - a description of the Application Context's role in the application and database. This becomes a comment in the database attached to the database role created for the context.

    • start_context - a required boolean value which establishes the default value of derived Instance Context (Msdata.SystInstanceContexts) start_context settings. When true, an Instance Context record derived from this Application Context will be, by default, started as active database connections when the parent Instance is started. False indicates that by default Instance startup will not establish database connections for the context. This value muse be set false for any Application Context defining a Datastore Owner Context or any other Context where the login_context is set false.

Examples

Updating an existing Application Context record

iex> app_context_id =
...>   MscmpSystInstance.get_application_context_id_by_name("ex_app2_updctx")
iex> update_params = %{
...>   display_name: "Updated Ex. App 2 Context",
...>   description: "A now updated description",
...>   start_context: false
...> }
iex> {:ok,
...>   %Msdata.SystApplicationContexts{
...>     display_name: "Updated Ex. App 2 Context",
...>     description: "A now updated description",
...>     start_context: false
...>   }} =
...>   MscmpSystInstance.update_application_context(app_context_id, update_params)

Runtime

Link to this function

start_all_applications(startup_options, opts \\ [])

@spec start_all_applications(map(), Keyword.t()) :: :ok | {:error, MscmpSystError.t()}

Starts all Applications and each Application's child Instances.

This function calls the start_application/3 function for each Application configured in the system. See the documentation for start_application/3 for more information about this function and the available parameters.

Link to this function

start_application(application, startup_options, opts \\ [])

@spec start_application(
  MscmpSystInstance.Types.application_id() | Msdata.SystApplications.t(),
  map(),
  Keyword.t()
) :: :ok | {:error, MscmpSystError.t()}

Starts the requested Application and its child Instances.

For the requested Application, start a DynamicSupervisor under which to supervise all of the services related to that Application including its child Instances.

Once the Application supervisor is started any child Instances which are in a start-eligible status are started under the Application supervisor. The Instance startup process starts each Instance asynchronously and concurrently. The instance startup process will also upgrade each Instance datastore to the current version of the application, if required. As such, you should be prepared for Application startup to be long running.

Parameters

  • application - either the record ID or the Msdata.SystApplications struct representing the Application to start.

  • startup_options - a map of values containing the Startup Options obtained from the MscmpSystOptions component.

  • opts - a Keyword List of optional values used during the initialization process. The available options include:

    • max_concurrency - the maximum number of Instances to start concurrently. This defaults to the greater of 1 or one quarter of the value returned by System.schedulers_online/0 floored.

    • other available options are passed to start_instance/3. See the documentation for start_instance/3 for the options it is able to accept.

    • registry_name - the name of the Registry service used to resolve the supervisors dynamically created by MscmpSystInstance. The default value is MscmpSystInstance.Registry.

    • instance_supervisor_name - the name of the supervisor to which all individual, dynamically created instance supervisors are children. The default value is MscmpSystInstance.InstanceSupervisor.

    • task_supervisor_name - on Instance startup, MscmpSystInstance will process Instance Datastore startup and migration application via concurrently Task processes. This option establishes the name of the supervisor under which these Task processes are started. The default value is MscmpSystInstance.TaskSupervisor.

Link to this function

start_instance(instance, startup_options, opts \\ [])

@spec start_instance(
  MscmpSystInstance.Types.instance_id() | Msdata.SystInstances.t(),
  map(),
  Keyword.t()
) :: :ok | {:error, MscmpSystError.t()}

Starts an Instance and places it under the appropriate Application's supervisor.

An Instance is essentially a tenant environment running a specific Application. Each environment's runtime characteristics are independent of other environments configured to run in the same system.

To be started the Instance must be in an Instance State of either functional type instance_states_initialized or instance_states_active. Trying to start an Instance in other Instance States will result in an error.

Note that we assume that the Application supervisor is already started and is ready to start child processes.

Parameters

  • instance - either the record ID or the Msdata.SystInstances struct of the Instance to start.

  • startup_options - a map of values containing the Startup Options obtained from the MscmpSystOptions component.

  • opts - a Keyword List of optional values used during the initialization process. The available options include:

    • migrating_state_id - the record ID of the Instance State to use while the starting Instance is being updated to the most current database definition of the application. If not provided the currently configured default Instance State for functional type instance_states_migrating will be used.

    • active_state_id - the record ID of the Instance State to use indicating that the Instance is ready to accept user workloads. If not provided the currently configured default Instance State for functional type instance_states_active will be used.

    • failure_state_id - the record ID of the Instance State to use indicating that the Instance startup process has failed and that the Instance is in an inconsistent state. If not provided the currently configured default Instance State for functional type instance_states_failure will be used.

    • migration_bindings - this a Keyword List containing values that will be substituted into the migration SQL files used to update a Datastore to the current version of the Application. Usually, there is no need to provide this option as the most common migration bindings are automatically generated from Instance record data.

    • registry_name - the name of the Registry service used to resolve the supervisors dynamically created by MscmpSystInstance. The default value is MscmpSystInstance.Registry.

    • instance_supervisor_name - the name of the supervisor to which all individual, dynamically created instance supervisors are children. The default value is MscmpSystInstance.InstanceSupervisor.

Link to this function

start_link(opts \\ [])

@spec start_link(Keyword.t()) :: Supervisor.on_start_child()

Starts the MscmpSystInstance services and supervisors.

In an application context, MscmpSystInstances sets up a supervisory tree and a process registry so that Application and Instance supervisors may be found for later use. At the top level, an Application Supervisor for each known Application is created and below that are Instance Supervisors for each Instance of the Application. Finally, child services such as processes for Datastores and other processes and supervisors as appropriate and specific to a given Instance are started under each Instance Supervisor.

This function allows the client application to link the MscmpSystInstance services to where it wishes within its own supervision tree using the normal Supervisor and DynamicSupervisor calls for starting children.

Parameters:

  • opts - A Keyword List of optional settings which override the established default values. The available options are:

    • supervisor_name - the name of the MscmpSystInstance root Supervisor to which other MscmpSystInstance processes and supervisors are bound. The default value is MscmpSystInstance.Supervisor.

    • registry_name - the name of the Registry service used to resolve the supervisors dynamically created by MscmpSystInstance. The default value is MscmpSystInstance.Registry.

    • instance_supervisor_name - the name of the supervisor to which all individual, dynamically created instance supervisors are children. The default value is MscmpSystInstance.InstanceSupervisor.

    • task_supervisor_name - on Instance startup, MscmpSystInstance will process Instance Datastore startup and migration application via concurrently Task processes. This option establishes the name of the supervisor under which these Task processes are started. The default value is MscmpSystInstance.TaskSupervisor.

Link to this function

stop_all_applications(opts \\ [])

@spec stop_all_applications(Keyword.t()) :: :ok | {:error, MscmpSystError.t()}

Stops all Applications, Application Supervisors, and their child Instances.

This function will call stop_application/2 for each Application configured in the system. For more information about the available options and behaviors of Application shutdown, please see the documentation for stop_application/2.

Link to this function

stop_application(application, opts \\ [])

@spec stop_application(
  MscmpSystInstance.Types.application_id() | Msdata.SystApplications.t(),
  Keyword.t()
) :: :ok | {:error, MscmpSystError.t()}

Shuts down the Supervisor process of the requested Application and all of its child processes, including Instances.

Parameters

  • application - either the record ID or the Msdata.SystApplications struct representing the Application to stop.

  • opts - a Keyword List of optional values used during the initialization process. The available options include:

    • supervisor_shutdown_timeout - a timeout value used to allow processes supervised by the Application supervisor to shut down cleanly. By default, this value is set to 60,000ms.

    • Other options available are defined by and passed to the stop_instance/2 function for Instance shutdown. See the stop_instance/2 for full information of the available options.

Link to this function

stop_instance(instance, opts \\ [])

@spec stop_instance(
  MscmpSystInstance.Types.instance_id() | Msdata.SystInstances.t(),
  Keyword.t()
) ::
  :ok | {:error, MscmpSystError.t()}

Stops an Instance.

Stopping an Instance shuts down its supervisor and any monitored processes.

Parameters

  • instance - either the record ID or the Msdata.SystInstances struct of the Instance to stop.

  • opts - a Keyword List of optional values used during the initialization process. The available options include:

    • supervisor_shutdown_timeout - a timeout value used to allow processes supervised by the Instance supervisor to shut down cleanly. By default, this value is set to 60,000ms.

    • db_shutdown_timeout - a timeout value used to limit the time allowed for a clean shutdown of the database connections used for operating Datastore. See the documentation for MscmpSystDb.stop_datastore/2 for more information.

    • registry_name - the name of the Registry service used to resolve the supervisors dynamically created by MscmpSystInstance. The default value is MscmpSystInstance.Registry.