CC : Unexpected behavior when using REST api to create landscape environment

I stumbled upon an unexpected behavior while using the rest api to create a new landscape environment.

How to reproduce:

1- I create an environment like this:

curl -u %ccUser%:%ccPassword% -X POST http://%ccServerHostname%:%ccServerPort%/cce/landscape/environments -d “alias=%ccEnv%”

2- I assign an installation to this environment (using UI or REST, the result is the same)

3- I execute the same REST command as step #1 to create the same environment again. Then instead of telling me that the environment already exists, it will delete the association made in step #2. In fact all associations previously made for this environment will all be lost as well.

This causes me a problem because I have a script that installs/configures an IS and creates everything it needs, and one of the first step is to create an environment in CC, then associate the new installation with the environment. The first time I run it it works fine. But if I run it again I will lose the previous association.

Is this behavior by design or is it a bug? (or maybe I’m not using the api correctly)

Thanks.

Yes, this is the current behavior on this API.

The behavior is debatable, but your script needs to handle either the current one or a potentially new behavior where the API would fail with 4xx error if the environment already exists.

The simplest way to handle either behavior is to avoid re-creating the environment if it already exists.
Below is a simple one liner to handle this:

cc list landscape environments | grep -q “ccEnv” || cc create landscape environments alias=“ccEnv”

Thanks
Sergei

1 Like

That’s a nice trick. I’ve just had to adapt it a little for Windows. I’m happy (and surprised) that double pipes are supported and have the same behavior as in Unix…

cc list landscape environments | findstr /i “ccEnv” || cc create landscape environments alias=“ccEnv”

Thanks.

1 Like