Beispiel #1
0
        public Task Load(ResourceDocument fullResourceState)
        {
            if (!TryGetIdentity(fullResourceState, out var identity))
            {
                throw new System.Exception("Identity field is required.");
            }

            if (!TryGetKindManager(fullResourceState, out var manager))
            {
                throw new System.Exception("Valid kind required.");
            }

            var newState = new ResourceState(
                manager, identity, fullResourceState
                );

            if (!Validate(newState))
            {
                throw new System.Exception("Invalid resource parameters specified.");
            }

            CommitResourceState(newState);

            return(newState.Manager.Save(newState));
        }
Beispiel #2
0
        public Task Apply(ResourceDocument partialResourceState)
        {
            if (!TryGetIdentity(partialResourceState, out var identity))
            {
                throw new System.Exception("Identity field is required.");
            }

            if (!TryGetResourceState(identity, out var existingState))
            {
                existingState = CreateDefaultState(identity, partialResourceState);
            }

            //  ignore any state in case it was present
            partialResourceState.State = null;

            var newState = ApplyFields(partialResourceState, existingState);

            if (!Validate(newState))
            {
                throw new System.Exception("Invalid resource parameters specified.");
            }

            CommitResourceState(newState);

            return(newState.Manager.Save(newState));
        }
Beispiel #3
0
 private ResourceState ApplyFields(ResourceDocument partialResourceState, ResourceState existingState)
 {
     return(new ResourceState(
                existingState.Manager,
                existingState.Identity,
                existingState.FullDocument.Patch(partialResourceState)
                ));
 }
Beispiel #4
0
        private bool TryGetKindManager(ResourceDocument partialResourceDocument, [NotNullWhen(true)] out ResourceManager?kindManager)
        {
            if (partialResourceDocument.Kind == null)
            {
                kindManager = default;
                return(false);
            }

            return(_resourceManagers.TryFind(q => q.Kind.GetKindDescriptor().Equals(partialResourceDocument.Kind.Value), out kindManager));
        }
Beispiel #5
0
        private ResourceState CreateDefaultState(string identity, ResourceDocument partialResourceState)
        {
            if (!TryGetKindManager(partialResourceState, out var manager))
            {
                throw new System.Exception("Valid kind required.");
            }

            //  todo: create default document from kind schema
            var stateDoc = new ResourceDocument(
                new ResourceDefinition(new List <ResourceField>
            {
                new ResourceField("identity", ResourceFieldValue.String(identity))
            }),
                kind: manager.Kind.GetKindDescriptor());

            return(new ResourceState(manager, identity, stateDoc));
        }
Beispiel #6
0
 private bool TryGetIdentity(ResourceDocument resourceDocument, [NotNullWhen(true)] out string?identity)
 {
     identity = resourceDocument.Definition["identity"]?.GetString();
     return(identity != null);
 }