<workflow>
	<rule match="$.services[?(@.type == 'demoService')].units[?(@.state.hostname and not @.temp.instanceName)]" desc="Units of demo service having hostname and image names assigned but without instances">
		<report entity="unit">
			<parameter name="id"><select path="id" /></parameter>
			<parameter name="text">Creating instance <select path="state.hostname" />(<select path="name" />)</parameter>
		</report>
		<update-cf-stack template="Linux" error="exception">
			<parameter name="mappings">
				<map>
					<mapping name="instanceName"><select path="state.hostname" /></mapping>
					<mapping name="environmentId"><select path="/id" /></mapping>
					<mapping name="userData">
						<prepare-user-data template="Demo" initFile="demo_init.sh">
							<parameter name="hostname"><select path="state.hostname" /></parameter>
							<parameter name="unit"><select path="id" /></parameter>
							<parameter name="service"><select path="::id" /></parameter>
						</prepare-user-data>
					</mapping>
					<mapping name="instanceType">
						<select path="::flavor" default="m1.medium" />
					</mapping>
					<mapping name="imageName">
						<select path="::osImage.name" />
					</mapping>
					<mapping name="availabilityZone">
						<select path="::availabilityZone" default="nova" />
					</mapping>
				</map>
			</parameter>
			<success>
				<set path="temp.instanceName">
					<select path="name" />
				</set>
				<report entity="unit">
					<parameter name="id"><select path="id" /></parameter>
					<parameter name="text">Instance <select path="state.hostname" />(<select path="name" />) created!</parameter>
				</report>
				<report entity="unit">
					<parameter name="id"><select path="id"/></parameter>
					<parameter name="text">Instance <select path="state.hostname"/> (<select path="name"/>) created</parameter>
				</report>
				<report entity="unit">
					<parameter name="id"><select path="id"/></parameter>
					<parameter name="text">Instance <select path="state.hostname"/> (<select path="name"/>) updated</parameter>
				</report>
				<report entity="unit">
					<parameter name="id"><select path="id"/></parameter>
					<parameter name="text"> Demo service installed on <select path="state.hostname"/> (<select path="name"/>)</parameter>
				</report>
			</success>
			<failure>
				<report entity="unit" level="error">
					<parameter name="id"><select path="id" /></parameter>
					<parameter name="text">Unable to deploy instance <select path="state.hostname" />(<select path="name" />) due to <format-error error="exception" /></parameter>
				</report>
				<stop />
			</failure>
		</update-cf-stack>
	</rule>
</workflow>
