Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for network polices #669

Merged
merged 6 commits into from
Nov 13, 2024
Merged

Conversation

bobbyiliev
Copy link
Contributor

@bobbyiliev bobbyiliev commented Nov 11, 2024

Fixes #666 😈

Adding a new materialize_network_policy resource:

resource "materialize_network_policy" "office_policy" {
  name = "office_access_policy"
  
  rule {
    name      = "new_york"
    action    = "allow"
    direction = "ingress"
    address   = "8.2.3.4/28"
  }

  rule {
    name      = "minnesota"
    action    = "allow"
    direction = "ingress"
    address   = "2.3.4.5/32"
  }

  comment = "Network policy for office locations"
}

Also adding a data source to allow users to retrieve all network policies:

data "materialize_network_policy" "all" {}

This also adds support for the new CREATENETWORKPOLICY system privilege, eg:

resource "materialize_role" "test" {
	name = "test_role"
}

resource "materialize_grant_system_privilege" "role_createnetworkpolicy" {
  role_name = materialize_role.test.name
  privilege = "CREATENETWORKPOLICY"
}

@bobbyiliev bobbyiliev marked this pull request as ready for review November 11, 2024 17:32
@bobbyiliev bobbyiliev requested a review from a team as a code owner November 11, 2024 17:32
@bobbyiliev bobbyiliev requested review from SangJunBak and removed request for a team November 11, 2024 17:32
@bobbyiliev bobbyiliev requested a review from ParkMyCar November 11, 2024 18:33
Copy link

@SangJunBak SangJunBak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall lgtm!

Rules []NetworkPolicyRule
}

type networkPolicyQueryResult struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed the casing here is different from the standard. Is that on purpose

return diag.FromErr(err)
}

SetId(string(region), "network_policies", "", "", d)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Noticed we use d.SetId in some places and SetId in other places. Any reason why we don't use d.SetId(transformIdWithRegion... here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, I've created that SetId helper function for all data sources that accept database or schema names. But in this case this is not needed so better to use d.SetId directly!

func SetId(region, resource, databaseName, schemaName string, d *schema.ResourceData) {
var id string
if databaseName != "" && schemaName != "" {
id = fmt.Sprintf("%s|%s|%s", databaseName, schemaName, resource)
} else if databaseName != "" {
id = fmt.Sprintf("%s|%s", databaseName, resource)
} else {
id = resource
}
d.SetId(utils.TransformIdWithRegion(region, id))
}

Copy link
Member

@ParkMyCar ParkMyCar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

High quality as always, thanks for getting this one across the line Bobby!

### Optional

- `comment` (String) **Public Preview** Comment on an object in the database.
- `region` (String) The region to use for the resource connection. If not set, the default region is used.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unfamiliar with the Terraform provider, is region a field we typically allow folks to specify?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this was introduced last year when we refactored the provider, this allows users to manage cross region instances within the same terraform project.

For example:

resource "materialize_database" "database" {
  name    = "example_database"
  comment = "database comment"
}

# Create in separate region
resource "materialize_database" "database_us_west" {
  name    = "example_database"
  comment = "database comment"
  region  = "aws/us-west-2"
}

the first one will create the db in the default region defined in the provider configuration, but the second one overrides the region and will create the db in aws/us-west-2 if it is enabled.

@@ -1,6 +1,7 @@
module github.com/MaterializeInc/terraform-provider-materialize

go 1.22.0
go 1.22.7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just double checking, is this kind of upgrade a breaking change? /compatible with how we're changing the version of the terraform provider?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this minor version upgrade should be ok, it was needed because of a recent dependabot commit:

4f61ea1

Kind of sneaking the bump into this PR 😅

Rules []byte `db:"rules"`
}

var networkPolicyQuery = NewBaseQuery(`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth making this a builtin view?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this will be quite helpful!

Comment on lines 115 to 117
// if err := d.Set("ownership_role", policy.OwnerName.String); err != nil {
// return diag.FromErr(err)
// }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to leave this in?

Comment on lines 171 to 179
// ownership not currently supported
// if v, ok := d.GetOk("ownership_role"); ok {
// ownership := materialize.NewOwnershipBuilder(metaDb, o)
// if err := ownership.Alter(v.(string)); err != nil {
// log.Printf("[DEBUG] resource failed ownership, dropping object: %s", o.Name)
// b.Drop()
// return diag.FromErr(err)
// }
// }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar to above, did you mean to leave this in?

@bobbyiliev
Copy link
Contributor Author

Thank you both @SangJunBak @ParkMyCar for the review! Very much appreciated 🙇

@bobbyiliev bobbyiliev merged commit 5be5c2b into main Nov 13, 2024
5 checks passed
@bobbyiliev bobbyiliev deleted the network-policy-configuration branch November 13, 2024 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support network policy configuration
3 participants