async def test_component_resource(self): rpc.register_resource_module("test", "index", MyResourceModule()) set_mocks(MyMocks()) res = MyComponentResource("test") urn = await res.urn.future() settings.SETTINGS.feature_support["resourceReferences"] = False deps = [] prop = await rpc.serialize_property(res, deps) self.assertListEqual([res], deps) self.assertEqual(urn, prop) settings.SETTINGS.feature_support["resourceReferences"] = True deps = [] prop = await rpc.serialize_property(res, deps) self.assertListEqual([res], deps) self.assertEqual(rpc._special_resource_sig, prop[rpc._special_sig_key]) self.assertEqual(urn, prop["urn"]) res = rpc.deserialize_properties(prop) self.assertTrue(isinstance(res, MyComponentResource)) rpc._RESOURCE_MODULES.clear() res = rpc.deserialize_properties(prop) self.assertEqual(urn, res)
def Diff(self, request, context): olds = rpc.deserialize_properties(request.olds, True) news = rpc.deserialize_properties(request.news, True) if news[PROVIDER_KEY] == rpc.UNKNOWN: provider = get_provider(olds) else: provider = get_provider(news) result = provider.diff(request.id, olds, news) # pylint: disable=no-member fields = {} if result.changes is not None: if result.changes: fields[ "changes" ] = proto.DiffResponse.DIFF_SOME # pylint: disable=no-member else: fields[ "changes" ] = proto.DiffResponse.DIFF_NONE # pylint: disable=no-member else: fields[ "changes" ] = proto.DiffResponse.DIFF_UNKNOWN # pylint: disable=no-member if result.replaces is not None: fields["replaces"] = result.replaces if result.delete_before_replace is not None: fields["deleteBeforeReplace"] = result.delete_before_replace return proto.DiffResponse(**fields)
def Check(self, request, context): olds = rpc.deserialize_properties(request.olds, True) news = rpc.deserialize_properties(request.news, True) if news[PROVIDER_KEY] == rpc.UNKNOWN: provider = get_provider(olds) else: provider = get_provider(news) result = provider.check(olds, news) inputs = result.inputs failures = result.failures inputs[PROVIDER_KEY] = news[PROVIDER_KEY] loop = asyncio.new_event_loop() inputs_proto = loop.run_until_complete( rpc.serialize_properties(inputs, {})) loop.close() failures_proto = [ proto.CheckFailure(f.property, f.reason) for f in failures ] fields = {"inputs": inputs_proto, "failures": failures_proto} return proto.CheckResponse(**fields)
def test_secret_push_up(self): secret_value = {rpc._special_sig_key: rpc._special_secret_sig, "value": "a secret value" } all_props = struct_pb2.Struct() all_props["regular"] = "a normal value" all_props["list"] = ["a normal value", "another value", secret_value] all_props["map"] = {"regular": "a normal value", "secret": secret_value} all_props["mapWithList"] = {"regular": "a normal value", "list": ["a normal value", secret_value]} all_props["listWithMap"] = [{"regular": "a normal value", "secret": secret_value}] val = rpc.deserialize_properties(all_props) self.assertEqual(all_props["regular"], val["regular"]) self.assertIsInstance(val["list"], dict) self.assertEqual(val["list"][rpc._special_sig_key], rpc._special_secret_sig) self.assertEqual(val["list"]["value"][0], "a normal value") self.assertEqual(val["list"]["value"][1], "another value") self.assertEqual(val["list"]["value"][2], "a secret value") self.assertIsInstance(val["map"], dict) self.assertEqual(val["map"][rpc._special_sig_key], rpc._special_secret_sig) self.assertEqual(val["map"]["value"]["regular"], "a normal value") self.assertEqual(val["map"]["value"]["secret"], "a secret value") self.assertIsInstance(val["mapWithList"], dict) self.assertEqual(val["mapWithList"][rpc._special_sig_key], rpc._special_secret_sig) self.assertEqual(val["mapWithList"]["value"]["regular"], "a normal value") self.assertEqual(val["mapWithList"]["value"]["list"][0], "a normal value") self.assertEqual(val["mapWithList"]["value"]["list"][1], "a secret value") self.assertIsInstance(val["listWithMap"], dict) self.assertEqual(val["listWithMap"][rpc._special_sig_key], rpc._special_secret_sig) self.assertEqual(val["listWithMap"]["value"][0]["regular"], "a normal value") self.assertEqual(val["listWithMap"]["value"][0]["secret"], "a secret value")
def RegisterResource(self, request, context): type_ = request.type name = request.name props = rpc.deserialize_properties(request.object) deps = sorted(list(request.dependencies)) parent = request.parent custom = request.custom protect = request.protect provider = request.provider delete_before_replace = request.deleteBeforeReplace ignore_changes = sorted(list(request.ignoreChanges)) version = request.version import_ = request.importId property_dependencies = {} for key, value in request.propertyDependencies.items(): property_dependencies[key] = sorted(list(value.urns)) outs = {} if type_ != "pulumi:pulumi:Stack": rrsig = signature(self.langhost_test.register_resource) args = [ context, self.dryrun, type_, name, props, deps, parent, custom, protect, provider, property_dependencies, delete_before_replace, ignore_changes, version, import_ ] outs = self.langhost_test.register_resource( *args[0:len(rrsig.parameters)]) if outs.get("urn"): urn = outs["urn"] self.registrations[urn] = { "type": type_, "name": name, "props": props } self.reg_count += 1 else: # Record the Stack's registration so that it can be the target of register_resource_outputs # later on. urn = self.langhost_test.make_urn(type_, "teststack") self.registrations[urn] = { "type": type_, "name": "somestack", "props": {} } return proto.RegisterResourceResponse(urn=urn, id="teststack", object=None) if "object" in outs: loop = asyncio.new_event_loop() obj_proto = loop.run_until_complete( rpc.serialize_properties(outs["object"], {})) loop.close() else: obj_proto = None return proto.RegisterResourceResponse(urn=outs.get("urn"), id=outs.get("id"), object=obj_proto)
def RegisterResource(self, request, context): type_ = request.type name = request.name props = rpc.deserialize_properties(request.object) deps = request.dependencies outs = {} if type_ != "pulumi:pulumi:Stack": outs = self.langhost_test.register_resource( context, self.dryrun, type_, name, props, deps) if outs.get("urn"): urn = outs["urn"] self.registrations[urn] = { "type": type_, "name": name, "props": props } self.reg_count += 1 if "object" in outs: loop = asyncio.new_event_loop() obj_proto = loop.run_until_complete( rpc.serialize_properties(outs["object"], [])) loop.close() else: obj_proto = None return proto.RegisterResourceResponse(urn=outs.get("urn"), id=outs.get("id"), object=obj_proto)
def Update(self, request, context): olds = rpc.deserialize_properties(request.olds) news = rpc.deserialize_properties(request.news) provider = get_provider(news) result = provider.update(request.id, olds, news) outs = {} if result.outs is not None: outs = result.outs outs[PROVIDER_KEY] = news[PROVIDER_KEY] loop = asyncio.new_event_loop() outs_proto = loop.run_until_complete(rpc.serialize_properties(outs, {})) loop.close() fields = {"properties": outs_proto} return proto.UpdateResponse(**fields)
def RegisterResourceOutputs(self, request, context): urn = request.urn outs = rpc.deserialize_properties(request.outputs) res = self.registrations.get(urn) if res: self.langhost_test.register_resource_outputs( context, self.dryrun, urn, res.t, res.name, res.props, outs) return empty_pb2.Empty()
def Invoke(self, request, context): args = rpc.deserialize_properties(request.args) failures, ret = self.langhost_test.invoke(context, request.tok, args, request.provider, request.version) failures_rpc = list(map( lambda fail: provider_pb2.CheckFailure(property=fail["property"], reason=fail["reason"]), failures)) loop = asyncio.new_event_loop() ret_proto = loop.run_until_complete(rpc.serialize_properties(ret, {})) loop.close() fields = {"failures": failures_rpc, "return": ret_proto} return proto.InvokeResponse(**fields)
async def _construct_inputs( request: proto.ConstructRequest) -> Dict[str, pulumi.Input[Any]]: def deps(key: str) -> Set[str]: return set(urn for urn in request.inputDependencies.get( key, proto.ConstructRequest.PropertyDependencies()).urns) return { k: await ProviderServicer._create_output(the_input, deps=deps(k)) for k, the_input in rpc.deserialize_properties( request.inputs, keep_unknowns=True).items() }
async def _construct_inputs( inputs: struct_pb2.Struct, input_dependencies: Any) -> Dict[str, pulumi.Input[Any]]: def deps(key: str) -> Set[str]: return set(urn for urn in input_dependencies.get( key, proto.ConstructRequest.PropertyDependencies()).urns) return { k: await ProviderServicer._select_value(the_input, deps=deps(k)) for k, the_input in rpc.deserialize_properties( inputs, keep_unknowns=True).items() }
def _construct_inputs( request: proto.ConstructRequest) -> Dict[str, pulumi.Output]: def deps(key: str) -> Set[Resource]: return set( DependencyResource(urn) for urn in request.inputDependencies.get( key, proto.ConstructRequest.PropertyDependencies()).urns) return { k: ProviderServicer._construct_output(the_input, deps=deps(k)) for k, the_input in rpc.deserialize_properties( request.inputs, keep_unknowns=True).items() }
def Create(self, request, context): props = rpc.deserialize_properties(request.properties) provider = get_provider(props) result = provider.create(props) outs = result.outs outs[PROVIDER_KEY] = props[PROVIDER_KEY] loop = asyncio.new_event_loop() outs_proto = loop.run_until_complete(rpc.serialize_properties(outs, {})) loop.close() fields = {"id": result.id, "properties": outs_proto} return proto.CreateResponse(**fields)
async def _call_args( request: proto.CallRequest) -> Dict[str, pulumi.Input[Any]]: def deps(key: str) -> Set[str]: return set(urn for urn in request.argDependencies.get( key, proto.CallRequest.ArgumentDependencies()).urns) return { k: await ProviderServicer._select_value(the_input, deps=deps(k)) for k, the_input in # We need to keep_internal, to keep the `__self__` that would normally be filtered because # it starts with "__". rpc.deserialize_properties( request.args, keep_unknowns=True, keep_internal=True).items() }
def test_internal_property(self): all_props = struct_pb2.Struct() all_props["a"] = "b" all_props["__defaults"] = [] all_props["c"] = {"foo": "bar", "__defaults": []} all_props["__provider"] = "serialized_dynamic_provider" all_props["__other"] = "baz" val = rpc.deserialize_properties(all_props) self.assertEqual({ "a": "b", "c": {"foo": "bar"}, "__provider": "serialized_dynamic_provider", }, val)
def Read(self, request, context): id_ = request.id props = rpc.deserialize_properties(request.properties) provider = get_provider(props) result = provider.read(id_, props) # pylint: disable=no-member outs = result.outs outs[PROVIDER_KEY] = props[PROVIDER_KEY] loop = asyncio.new_event_loop() outs_proto = loop.run_until_complete(rpc.serialize_properties(outs, {})) loop.close() fields = {"id": result.id, "properties": outs_proto} return proto.ReadResponse(**fields)
def ReadResource(self, request, context): type_ = request.type name = request.name id_ = request.id parent = request.parent state = rpc.deserialize_properties(request.properties) outs = self.langhost_test.read_resource(context, type_, name, id_, parent, state) if "properties" in outs: loop = asyncio.new_event_loop() props_proto = loop.run_until_complete(rpc.serialize_properties(outs["properties"], {})) loop.close() else: props_proto = None return proto.ReadResourceResponse( urn=outs.get("urn"), properties=props_proto)
def RegisterResource(self, request, context): type_ = request.type name = request.name props = rpc.deserialize_properties(request.object) deps = list(request.dependencies) parent = request.parent custom = request.custom protect = request.protect provider = request.provider outs = {} if type_ != "pulumi:pulumi:Stack": outs = self.langhost_test.register_resource( context, self.dryrun, type_, name, props, deps, parent, custom, protect, provider) if outs.get("urn"): urn = outs["urn"] self.registrations[urn] = { "type": type_, "name": name, "props": props } self.reg_count += 1 else: # Record the Stack's registration so that it can be the target of register_resource_outputs # later on. urn = self.langhost_test.make_urn(type_, "teststack") self.registrations[urn] = { "type": type_, "name": "somestack", "props": {} } return proto.RegisterResourceResponse(urn=urn, id="teststack", object=None) if "object" in outs: loop = asyncio.new_event_loop() obj_proto = loop.run_until_complete( rpc.serialize_properties(outs["object"], [])) loop.close() else: obj_proto = None return proto.RegisterResourceResponse(urn=outs.get("urn"), id=outs.get("id"), object=obj_proto)
def Invoke(self, request, context): args = rpc.deserialize_properties(request.args) failures, ret = self.langhost_test.invoke(context, request.tok, args) return proto.InvokeResponse(failures=failures, ret=ret)
def Delete(self, request, context): id_ = request.id props = rpc.deserialize_properties(request.properties) provider = get_provider(props) provider.delete(id_, props) return empty_pb2.Empty()