Adding a channel via API

We are finally getting around to using Channels. We have a bunch of microservice projects that make up an overall project. I’ve debated whether to put them all into one project or multiples and settled on multiple, with one for each for now. However that means when we move to a new release (release in this case means set of requirements/defects/etc., not the release as seen in Octopus per se) and I have a need to create a new channel, unless I’m missing something I need to create a new channel in each. As such I was looking at scripting this so I don’t have to click buttons all over the place.

So I tried the following:

$ChannelResource = new-object Octopus.Client.Model.ChannelResource
$ChannelResource.Description = $ReleaseNumber
$ChannelResource.Name = $ReleaseNumber
$ChannelResource.ProjectId = $ProjectItem.Id
$ChannelResource.IsDefault = $false
$ChannelResource.LifecycleId = $LifeCycles.Id
$ChannelResource.Rules = New-Object Octopus.Client.Model.ChannelVersionRuleResource
$Channel = $repository.Channels.Create($ChannelResource)

When I do that I get this. I don’t see a reference anywhere to source. I thought maybe I had an authentication issue, but I am authenticated, that’s how I get a ProjectID and LifeCycleID. Any suggestions?

Exception calling "Create" with "1" argument(s): "Octopus Server returned an error: Value cannot be null.
Parameter name: source
Server exception: 
System.ArgumentNullException: Value cannot be null.
Parameter name: source
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
   at System.Linq.Enumerable.All[TSource](IEnumerable`1 source, Func`2 predicate)
   at FluentValidation.Validators.PredicateValidator.IsValid(PropertyValidatorContext context) in 
c:\Projects\FluentValidation\src\FluentValidation\Validators\PredicateValidator.cs:line 37
   at FluentValidation.Validators.PropertyValidator.Validate(PropertyValidatorContext context) in 
c:\Projects\FluentValidation\src\FluentValidation\Validators\PropertyValidator.cs:line 71
   at FluentValidation.Internal.PropertyRule.<Validate>d__10.MoveNext() in c:\Projects\FluentValidation\src\FluentValidation\Internal\PropertyRule.cs:line 
241
   at System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at FluentValidation.AbstractValidator`1.Validate(ValidationContext`1 context) in 
c:\Projects\FluentValidation\src\FluentValidation\AbstractValidator.cs:line 113
   at Octopus.Server.Web.Infrastructure.Api.Responder`1.ValidateModel[TModel](IEnumerable`1 validators, TModel model) in 
Y:\work\refs\tags\3.3.6\source\Octopus.Server\Web\Infrastructure\Api\Responder.cs:line 236
   at Octopus.Server.Web.Infrastructure.Api.CreateResponseDescriptor`2.Responder.Execute() in 
Y:\work\refs\tags\3.3.6\source\Octopus.Server\Web\Infrastructure\Api\CreateResponseDescriptor.cs:line 55
   at Octopus.Server.Web.Infrastructure.Api.Responder`1.Respond(TDescriptor options, NancyContext context) in 
Y:\work\refs\tags\3.3.6\source\Octopus.Server\Web\Infrastructure\Api\Responder.cs:line 162
   at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
   at CallSite.Target(Closure , CallSite , Object , Object , NancyContext )
   at Octopus.Server.Web.Api.OctopusRestApiModule.<>c__DisplayClass0_0.<.ctor>b__0(Object o) in 
Y:\work\refs\tags\3.3.6\source\Octopus.Server\Web\Api\OctopusRestApiModule.cs:line 46
   at CallSite.Target(Closure , CallSite , Func`2 , Object )
   at Nancy.Routing.Route.<>c__DisplayClass4.<Wrap>b__3(Object parameters, CancellationToken context)
-----------------------
"
At line:88 char:1
+ $Channel = $repository.Channels.Create($ChannelResource)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : OctopusServerException

Hi Mike,

I believe this error is occurring because of the blank ChannelVersionRuleResource object. If you sub that for an empty array @() instead (or complete the fields for the ChannelVersionRuleResource), the create operation should succeed.

Eg.

# Testing new channel creation (no rules)

$project = New-Object Octopus.Client.Model.ProjectResource
$project.Id = "Projects-581"
$lifecycle = New-Object Octopus.Client.Model.LifecycleResource
$lifecycle.Id = "Lifecycles-21"

$guid = [GUID]::NewGuid().ToString().Substring(0,8)
$channelResource1 = new-object Octopus.Client.Model.ChannelResource
$channelResource1.Description = "test channel - $($guid)"
$channelResource1.Name = "test channel - $($guid)"
$channelResource1.ProjectId = $project.Id
$channelResource1.IsDefault = $false
$channelResource1.LifecycleId = $lifecycle.Id
$channelResource1.Rules = @()
$channel1 = $repository.Channels.Create($channelResource1)

Let me know how you go :slight_smile:

Cheers
Mark

Awesome!!! That worked!

Mike