Hi, How can I get the currently used environment from the API?

I see in the documentation the following existing endpoint used to "Get the name of the current environment":

/rest/v1/grorup/name/<groupName>/project/name/<projectName>/environment/name/<environmentName>/name

 

But I don't understand this endpoint if to call it I need to know the name of the environment, when it's actually the information I'm looking for.

 

Here's the link of the documentation: https://documentation.matillion.com/docs/2768621

 

Could somebody help me?

 

Thank you in advance,

 

Cyrille Dakhlia

Hi Cyrille,

 

I agree that particular endpoint is unusual, because - as you say - you have to provide the name of the environment in the path, and then it returns the same name back to you. It's mainly there for completeness, alongside several other endpoints which return information about the Environment.

 

There's maybe a use for it in a DevOps scenario. Say you have a Dev-Test-Production pipeline (perhaps in Azure DevOps like this example), and you wish to assert that a certain Environment exists in Production before deploying. In that case you could use the /Name endpoint and check that it's successful before deploying. If the Environment does not exist, the API call will return a message like below (which I ran from a Bash Script). You could detect that and halt the deployment before it breaks anything.

 

 

Best regards,

Ian

 

I agree that makes no sense. Have tried leaving off the last part: "http://<InstanceAddress>/rest/v1/group/name/<groupname>/project/name/<projectName>/environment/name" to see if that gives you the current environment?

Thank you two for your answers.

@ian.funnell​ indeed it can be used for that purpose. Another one that came to my mind is when retrieving a list of environments, using that endpoint helps to know which one is the one we're looking for in that list.

@DataGuy​ I haven't tried unfortunately, and my trial has expired.

I have seen when exporting a project with the following endpoint rest/v1/group/name/<groupName>/project/name/<projectName>/export that there is a field named "environmentExports" which is a list, but in my sample it only contains one environment export. Does that field lists all environments defined in the Matillion instance or are environments bound to a project, and that list only shows the environments of the specified project?

Just tried "http://<InstanceAddress>/rest/v1/group/name/<groupname>/project/name/<projectName>/environment" and it gave me the current environment. Looks like you need to leave off "/name"

To answer your question on environment, environments are bound to a project, but you can have more than one environment defined for a prject.

For clarity, all the API calls really are RESTful, meaning

  • Every call is stateless, and is independent from any other REST API call (past or future), and from whatever else is going on with sessions logged into the UI
  • Because they are stateless, every call must contain enough information for Matillion to respond

 

So for example, if you make a REST call and ask "Give me the Environment name", the call will fail because it won't know which Environment you mean.

And you can't tell it "Give me the Environment name, and I mean the one I am (or was) logged into in the UI". The call will still fail, because it's deliberately not permitted to infer anything from whatever may or may not be going on with sessions logged into the UI.

As you can probably guess, you also can't tell it "Give me the Environment name, and I mean the one I specified in a previous API call"... for similar reasons.

 

So, here's a summary of the endpoints we have been discussing:

 

/rest/v1/group/name/<group>/project/name/<project>/environment

Valid and returns an array of all the Environments.

You're not asking for any one in particular, and there can be more than one.

 

/rest/v1/group/name/<group>/project/name/<project>/environment/name

Not a valid endpoint, and fails with a 404. The /name on the end implies that an Environment name must follow.

 

/rest/v1/group/name/<group>/project/name/<project>/environment/name/<envname>

Not a valid endpoint, because you have not declared what information you want back about the Environment

Arguably we could have made it return some chunk of JSON. But as it is, it (rather confusingly) succeeds and returns a big JSON description of the valid endpoints

 

/rest/v1/group/name/<group>/project/name/<project>/environment/name/<envname>/name

Vaild, and returns the Name of the Environment

 

/rest/v1/group/name/<group>/project/name/<project>/environment/name/<envname>/id

Vaild, and returns the ID of the Environment

 

Hope that makes sense and is helpful!

Ian

 

Hello @DataGuy​, @ian.funnell​ !

I am back here because I could have the screenshot illustrating what I was looking for.

In the picture, we can see it is written "[Selected]" attached to the environment "redshift-environment",

but in the "Manage Project" panel we can see that the "Default Environment" is defined on "environment-2".

I have understood how to retrieve the default environment, but regarding the "[Selected]" environment, there is absolutely no means to get it from the API? 😮

Sorry for coming back to an old question!

Best regards,

Cyrille Dakhlia

Thank you for your help! I really appreciate.

 

In that case, I am wondering something... does it mean that if you switch to another environment using the menus, the response API will change consequently?

When using the UI it is pretty obvious that changing the environment will effectively change the environment.

But when using the API, are the selected variables saved in memory? What I mean is that if I disconnect from my Matillion instance but still let it turned on, will the API give me as an answer the last selected environment?

By the way, when clicking on the link you shared

"http://<InstanceAddress>/rest/v1/group/name/<groupname>/project/name/<projectName>/environment"

it actually calls me the following one:

"http://<InstanceAddress>/rest/v1/group/name/<groupname>/project/name/<projectName>/environment/name"​

 

​So this should be the working endpoint?

Thank you for your answer!

Thank you very much @ian.funnell​ for your answer, you made it very clear and complete!

I think it answers my question which, I realise, doesn't have the right approach...

I will open a new discussion for the real question behind the question I have asked here since this one has been answered.

Thank you again all!

Hi Cyrille,

 

No problem at all to continue the discussion, and in fact I found something I was not expecting which is always good!

 

As you have seen, when you click the "Select Environment" option in the Matillion UI, it changes your "selected" environment, and highlights it.

 

But it also changes the "selected" environment for all other sessions which are logged into the same project. That means it's a property which is held on the Matillion server, at the level of "project". Not at the level of session like I was expecting.

 

Now, you can find the default environment using the REST API, for example like this:

 

curl -s -u un:pw http://127.0.0.1:8080/rest/v1/group/name/YourGroupName/project/name/YourProjectName/export | jq '.objects[0].environmentExports.objects[] | select(.defaultEnvironment==true) | .name'

 

But you can not do the same to find the "selected" environment, because the /export JSON does not contain that information.

 

So if it's potentially useful, you might add an Idea to our portal, asking for something like a "selectedEnvironment": boolean attribute to be added to that endpoint?

 

Thanks for continuing an interesting discussion!

 

Ian

 

Thank you very much Ian, it is always a real pleasure to discuss with obliging people like you!

 

This eventually concludes this series of messages and answers my original question.

I keep in mind to add it in the Ideas Portal in case I cannot cope without that information!

 

Thank you again!

 

Cyrille