|
| 1 | +package projectipaccesslist_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 9 | + "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" |
| 10 | + "github.com/mongodb/terraform-provider-mongodbatlas/internal/service/projectipaccesslist" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "go.mongodb.org/atlas-sdk/v20231115002/admin" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + AWSSecurityGroup = "AWSSecurityGroup" |
| 17 | + CIDRBlock = "CIDRBlock" |
| 18 | + IPAddress = "IPAddress" |
| 19 | + Comment = "Comment" |
| 20 | + GroupID = "GroupID" |
| 21 | + IPAddressID = conversion.EncodeStateID(map[string]string{ |
| 22 | + "entry": IPAddress, |
| 23 | + "project_id": GroupID, |
| 24 | + }) |
| 25 | + CidrBlockID = conversion.EncodeStateID(map[string]string{ |
| 26 | + "entry": CIDRBlock, |
| 27 | + "project_id": GroupID, |
| 28 | + }) |
| 29 | + AwsSecurityGroupID = conversion.EncodeStateID(map[string]string{ |
| 30 | + "entry": AWSSecurityGroup, |
| 31 | + "project_id": GroupID, |
| 32 | + }) |
| 33 | +) |
| 34 | + |
| 35 | +func TestNewMongoDBProjectIPAccessList(t *testing.T) { |
| 36 | + testCases := []struct { |
| 37 | + tfModel *projectipaccesslist.TfProjectIPAccessListModel |
| 38 | + expectedResult *[]admin.NetworkPermissionEntry |
| 39 | + name string |
| 40 | + }{ |
| 41 | + { |
| 42 | + name: "NewMongoDBProjectIPAccessList", |
| 43 | + tfModel: &projectipaccesslist.TfProjectIPAccessListModel{ |
| 44 | + AWSSecurityGroup: types.StringValue(AWSSecurityGroup), |
| 45 | + CIDRBlock: types.StringValue(CIDRBlock), |
| 46 | + IPAddress: types.StringValue(IPAddress), |
| 47 | + Comment: types.StringValue(Comment), |
| 48 | + }, |
| 49 | + expectedResult: &[]admin.NetworkPermissionEntry{ |
| 50 | + { |
| 51 | + AwsSecurityGroup: &AWSSecurityGroup, |
| 52 | + CidrBlock: &CIDRBlock, |
| 53 | + IpAddress: &IPAddress, |
| 54 | + Comment: &Comment, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tc := range testCases { |
| 61 | + t.Run(tc.name, func(t *testing.T) { |
| 62 | + resultModel := projectipaccesslist.NewMongoDBProjectIPAccessList(tc.tfModel) |
| 63 | + |
| 64 | + assert.Equal(t, tc.expectedResult, resultModel) |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestNewTfProjectIPAccessListModel(t *testing.T) { |
| 70 | + testCases := []struct { |
| 71 | + tfModel *projectipaccesslist.TfProjectIPAccessListModel |
| 72 | + sdkModel *admin.NetworkPermissionEntry |
| 73 | + expectedResult *projectipaccesslist.TfProjectIPAccessListModel |
| 74 | + name string |
| 75 | + }{ |
| 76 | + { |
| 77 | + name: "NewTfProjectIPAccessListModel with IpAddress", |
| 78 | + tfModel: &projectipaccesslist.TfProjectIPAccessListModel{ |
| 79 | + Timeouts: timeouts.Value{}, |
| 80 | + }, |
| 81 | + sdkModel: &admin.NetworkPermissionEntry{ |
| 82 | + IpAddress: &IPAddress, |
| 83 | + Comment: &Comment, |
| 84 | + GroupId: &GroupID, |
| 85 | + }, |
| 86 | + expectedResult: &projectipaccesslist.TfProjectIPAccessListModel{ |
| 87 | + ID: types.StringValue(IPAddressID), |
| 88 | + ProjectID: types.StringValue(GroupID), |
| 89 | + AWSSecurityGroup: types.StringValue(""), |
| 90 | + CIDRBlock: types.StringValue(""), |
| 91 | + IPAddress: types.StringValue(IPAddress), |
| 92 | + Comment: types.StringValue(Comment), |
| 93 | + Timeouts: timeouts.Value{}, |
| 94 | + }, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "NewTfProjectIPAccessListModel with CidrBlock", |
| 98 | + tfModel: &projectipaccesslist.TfProjectIPAccessListModel{ |
| 99 | + Timeouts: timeouts.Value{}, |
| 100 | + }, |
| 101 | + sdkModel: &admin.NetworkPermissionEntry{ |
| 102 | + CidrBlock: &CIDRBlock, |
| 103 | + IpAddress: &IPAddress, |
| 104 | + Comment: &Comment, |
| 105 | + GroupId: &GroupID, |
| 106 | + }, |
| 107 | + expectedResult: &projectipaccesslist.TfProjectIPAccessListModel{ |
| 108 | + ID: types.StringValue(CidrBlockID), |
| 109 | + ProjectID: types.StringValue(GroupID), |
| 110 | + AWSSecurityGroup: types.StringValue(""), |
| 111 | + CIDRBlock: types.StringValue(CIDRBlock), |
| 112 | + IPAddress: types.StringValue(IPAddress), |
| 113 | + Comment: types.StringValue(Comment), |
| 114 | + Timeouts: timeouts.Value{}, |
| 115 | + }, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "NewTfProjectIPAccessListModel with AwsSecurityGroup", |
| 119 | + tfModel: &projectipaccesslist.TfProjectIPAccessListModel{ |
| 120 | + Timeouts: timeouts.Value{}, |
| 121 | + }, |
| 122 | + sdkModel: &admin.NetworkPermissionEntry{ |
| 123 | + AwsSecurityGroup: &AWSSecurityGroup, |
| 124 | + IpAddress: &IPAddress, |
| 125 | + Comment: &Comment, |
| 126 | + GroupId: &GroupID, |
| 127 | + }, |
| 128 | + expectedResult: &projectipaccesslist.TfProjectIPAccessListModel{ |
| 129 | + ID: types.StringValue(AwsSecurityGroupID), |
| 130 | + ProjectID: types.StringValue(GroupID), |
| 131 | + AWSSecurityGroup: types.StringValue(AWSSecurityGroup), |
| 132 | + CIDRBlock: types.StringValue(""), |
| 133 | + IPAddress: types.StringValue(IPAddress), |
| 134 | + Comment: types.StringValue(Comment), |
| 135 | + Timeouts: timeouts.Value{}, |
| 136 | + }, |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + for _, tc := range testCases { |
| 141 | + t.Run(tc.name, func(t *testing.T) { |
| 142 | + resultModel := projectipaccesslist.NewTfProjectIPAccessListModel(tc.tfModel, tc.sdkModel) |
| 143 | + |
| 144 | + assert.Equal(t, tc.expectedResult, resultModel) |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestNewTfProjectIPAccessListDSModel(t *testing.T) { |
| 150 | + testCases := []struct { |
| 151 | + sdkModel *admin.NetworkPermissionEntry |
| 152 | + expectedResult *projectipaccesslist.TfProjectIPAccessListDSModel |
| 153 | + name string |
| 154 | + }{ |
| 155 | + { |
| 156 | + name: "NewTfProjectIPAccessListDSModel with IpAddress", |
| 157 | + sdkModel: &admin.NetworkPermissionEntry{ |
| 158 | + AwsSecurityGroup: &AWSSecurityGroup, |
| 159 | + CidrBlock: &CIDRBlock, |
| 160 | + IpAddress: &IPAddress, |
| 161 | + Comment: &Comment, |
| 162 | + GroupId: &GroupID, |
| 163 | + }, |
| 164 | + expectedResult: &projectipaccesslist.TfProjectIPAccessListDSModel{ |
| 165 | + ID: types.StringValue(IPAddressID), |
| 166 | + ProjectID: types.StringValue(GroupID), |
| 167 | + AWSSecurityGroup: types.StringValue(AWSSecurityGroup), |
| 168 | + CIDRBlock: types.StringValue(CIDRBlock), |
| 169 | + IPAddress: types.StringValue(IPAddress), |
| 170 | + Comment: types.StringValue(Comment), |
| 171 | + }, |
| 172 | + }, |
| 173 | + { |
| 174 | + name: "NewTfProjectIPAccessListDSModel with CidrBlock", |
| 175 | + sdkModel: &admin.NetworkPermissionEntry{ |
| 176 | + CidrBlock: &CIDRBlock, |
| 177 | + Comment: &Comment, |
| 178 | + GroupId: &GroupID, |
| 179 | + }, |
| 180 | + expectedResult: &projectipaccesslist.TfProjectIPAccessListDSModel{ |
| 181 | + ID: types.StringValue(CidrBlockID), |
| 182 | + ProjectID: types.StringValue(GroupID), |
| 183 | + AWSSecurityGroup: types.StringValue(""), |
| 184 | + CIDRBlock: types.StringValue(CIDRBlock), |
| 185 | + IPAddress: types.StringValue(""), |
| 186 | + Comment: types.StringValue(Comment), |
| 187 | + }, |
| 188 | + }, |
| 189 | + { |
| 190 | + name: "NewTfProjectIPAccessListDSModel with AwsSecurityGroup", |
| 191 | + sdkModel: &admin.NetworkPermissionEntry{ |
| 192 | + AwsSecurityGroup: &AWSSecurityGroup, |
| 193 | + CidrBlock: &CIDRBlock, |
| 194 | + Comment: &Comment, |
| 195 | + GroupId: &GroupID, |
| 196 | + }, |
| 197 | + expectedResult: &projectipaccesslist.TfProjectIPAccessListDSModel{ |
| 198 | + ID: types.StringValue(AwsSecurityGroupID), |
| 199 | + ProjectID: types.StringValue(GroupID), |
| 200 | + AWSSecurityGroup: types.StringValue(AWSSecurityGroup), |
| 201 | + CIDRBlock: types.StringValue(CIDRBlock), |
| 202 | + IPAddress: types.StringValue(""), |
| 203 | + Comment: types.StringValue(Comment), |
| 204 | + }, |
| 205 | + }, |
| 206 | + } |
| 207 | + |
| 208 | + for _, tc := range testCases { |
| 209 | + t.Run(tc.name, func(t *testing.T) { |
| 210 | + resultModel, _ := projectipaccesslist.NewTfProjectIPAccessListDSModel(context.Background(), tc.sdkModel) |
| 211 | + |
| 212 | + assert.Equal(t, tc.expectedResult, resultModel) |
| 213 | + }) |
| 214 | + } |
| 215 | +} |
0 commit comments