Cannot create scoped user roles and find user roles by name via Octopus.Client

Hi, im trying to add scoped user roles through Octopus.Client in .NET Core framework and I am getting the following error:


Unhandled Exception: Octopus.Client.Exceptions.OctopusValidationException: There was a problem with your request.

  • One or more referenced resources do not exist: “Projects-1139” provided for ProjectIds; “Environments-281” provided for EnvironmentIds; “Environments-282” provided for EnvironmentIds

    at Octopus.Client.OctopusClient.DispatchRequest[TResponseResource](OctopusRequest request, Boolean readResponse)
    at Octopus.Client.OctopusClient.Create[TResource](String path, TResource resource, Object pathParameters)


Happens at the line repository.ScopedUserRoles.Create(role);
I can confirm that thee project and environments do exist, so the error message doesn’t help me much :frowning:

On top of this, I also get an error using the command repository.UserRoles.FindByName(userrole.RoleName) . With the error being:
Unhandled Exception: Octopus.Client.Exceptions.OctopusDeserializationException: Unable to process response from server: Error converting value “PackageMetadataPush” to type ‘Octopus.Client.Model.Permission’. Path ‘Items[0].GrantedSpacePermissions[9]’, line 42, position 29… Response content: {
“ItemType”: “UserRole”,
“TotalResults”: 36,
“ItemsPerPage”: 30,
“NumberOfPages”: 2,
—> Newtonsoft.Json.JsonSerializationException: Error converting value “PackageMetadataPush” to type ‘Octopus.Client.Model.Permission’. Path ‘Items[0].GrantedSpacePermissions[9]’, line 42, position 29. —> System.ArgumentException: Requested value ‘PackageMetadataPush’ was not found.
at Newtonsoft.Json.Utilities.EnumUtils.ParseEnum(Type enumType, String value, Boolean disallowNumber)
at Newtonsoft.Json.Converters.StringEnumConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
— End of inner exception stack trace —
at Newtonsoft.Json.Converters.StringEnumConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolvePropertyAndCreatorValues(JsonObjectContract contract, JsonProperty containerProperty, JsonReader reader, Type objectType)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor`1 creator, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Octopus.Client.OctopusClient.DispatchRequest[TResponseResource](OctopusRequest request, Boolean readResponse)
— End of inner exception stack trace —

Hi @shanis.lovin,

Thanks for reaching out! Couple of questions about your script/code

  • Which version of Octopus.Client.Dll are you using?
  • Are you setting the right space at the beggining of your code? If the code is being executed against the wrong space, that might explain why it can’t find some Projects/Environments. The below snipet shows how to switch to the right Space (by creating a new repository scoped to the space you wanna use)
        static void Main(string[] args)
        {
            var ApiKey = "API-WCOC1PDSEH4M2EIHPUAMONMWZMK";
            var Server = "http://localhost";
            var spaceName = "TheSpaceIActuallyWantToUse";
            
            var baseRepository = new OctopusRepository(new OctopusServerEndpoint(apiKey: ApiKey, octopusServerAddress: Server));
            var mySpace = baseRepository.Spaces.FindByName(spaceName);
            var spaceRepository = new OctopusRepository(new OctopusServerEndpoint(apiKey: ApiKey, octopusServerAddress: Server), RepositoryScope.ForSpace(mySpace));
            
            spaceRepository.Projects.FindAll().ToList().ForEach( x => Console.WriteLine(x.Name));
            spaceRepository.Environments.FindAll().ToList().ForEach( x => Console.WriteLine(x.Name));
        }

Let me know if that helps!

Dalmiro

Hi Dalmiro,

We are on Octopus.Client version 6.0.2.0 :slight_smile:

I’ve checked the spaces on our server, there is only one space called default space. I tried using this space as the repo but objects that came before with the Find commands no longer return anything.

When I dont specify the space and i run the following code:


Console.WriteLine(_repository.Projects.Get(“Projects-1139”));
Console.WriteLine(_repository.Environments.Get(“Environments-281”));

_repository.ScopedUserRoles.Create(role);


I get a project and environment object returned from both console logs. And then at the create role command I get the error saying both of those referenced resources do not exist :s

Hi @shanis.lovin,

I believe the Error converting value “PackageMetadataPush” to type ‘Octopus.Client.Model.Permission’. is happening because the Octopus.client version 6.0.2 still didn’t have the PackageMetadataPush permission which was introduced later on that same month (march 2019).

(nuget.org screenshot)

(commit that introduced this new permission)

I’d recommend you to upgrade to either the latest version of OCtopus.client or at least to 6.1.3 which should have that permission on code.

Let me know how that goes.
Dalmiro

Thanks Dalmiro. I’ll try updating Octopus.Client.

*Update: upgrading to 6.1.3 did not work but 7.0.0 did :slight_smile: Thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.