Terraform querying non-existing data sources

Hi there, I have the next problem, maybe trivial, but I cannot wrap my head around it.
I have a need to query the existing OpsGenie team by a given name and only if the team that I am trying to create, does not exist I will create it.
I do believe there should be a way for doing so in a convenient way in the TF using some conditional logic, but unfortunately enough, I couldn’t find this way yet.
Please let me know if it is surmountable at all.
Here is a small example of what I was talking about:
provider “opsgenie” {
api_key = “XXXXXXXXXXXXXXXXXX”
api_url = “api.opsgenie.XXX.com
}
data “opsgenie_team” “checking-the-team” {
name = “abra-kadabra”
}
resource “opsgenie_team” “new_team” {
name = data.opsgenie_team.checking-the-team.id != “” ? data.opsgenie_team.checking-the-team.id : “abra-kadabra”
}

This is what I get when I use terraform plan:
Error: Error occurred with Status code: 404, Message: No team exists with name [abra-kadabra], Took: 0.009000…

I would like to get an empty value for a non-existing data source, so please let me if any of you have already dealt with the issue I am trying to describe.

That isn’t possible. Instead you’d create a resource to create/manage the team, and if it already exists use terraform import to add it into the state so it is managed by Terraform.

Well, anyway thanks for letting me know. I would try to write some Python module checking the team existence and returning some Boolean value based on that.
And if it pans out for me, I’ll publish here my findings.
This is because I am dealing with the automation and terraform’s import is mainly manual.

Thanks a lot.
Best Wishes,
Michael

The need to import should be fairly infrequent as the expectation would
be that Terraform is going to create teams if it is reponsible for
managing them.

I’m not sure I understand what your Python script would be for? If you
are wanting to manage a resource that already exists within Terraform
you need to run terraform import.

There’s some more background information and suggestions about this in the Terraform docs section Conditional Creation of Objects, which is a part of the broader guide Module Composition.