|
| 1 | +package convert |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/PaloAltoNetworks/terraform-provider-prismacloudcompute/internal/api/policy" |
| 5 | + "github.com/PaloAltoNetworks/terraform-provider-prismacloudcompute/internal/api/waas" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | +) |
| 8 | + |
| 9 | +func SchemaToFirewallAppPolicy(d *schema.ResourceData) (policy.FirewallAppPolicy, error) { |
| 10 | + policy := policy.FirewallAppPolicy{} |
| 11 | + |
| 12 | + if val, ok := d.GetOk("max_port"); ok { |
| 13 | + policy.MaxPort = val.(int) |
| 14 | + } |
| 15 | + |
| 16 | + if val, ok := d.GetOk("min_port"); ok { |
| 17 | + policy.MinPort = val.(int) |
| 18 | + } |
| 19 | + |
| 20 | + if schemaRules, ok := d.GetOk("rules"); ok { |
| 21 | + iterRules := schemaRules.([]interface{}) |
| 22 | + for _, schemaRule := range iterRules { |
| 23 | + mapRule := schemaRule.(map[string]interface{}) |
| 24 | + rule := waas.WaasRule{} |
| 25 | + |
| 26 | + if val, ok := mapRule["allow_malformed_http_header_names"]; ok { |
| 27 | + rule.AllowMalformedHttpHeaderNames = val.(bool) |
| 28 | + } |
| 29 | + |
| 30 | + if schemaApplications, ok := mapRule["applications"]; ok { |
| 31 | + iterApplications := schemaApplications.([]interface{}) |
| 32 | + for _, schemaApplication := range iterApplications { |
| 33 | + mapApplication := schemaApplication.(map[string]interface{}) |
| 34 | + |
| 35 | + if schemaAPIs, ok := mapApplication["api"]; ok { |
| 36 | + iterAPIs := schemaAPIs.([]interface{}) |
| 37 | + for _, schemaAPI := range iterAPIs { |
| 38 | + mapAPI := schemaAPI.(map[string]interface{}) |
| 39 | + api := waas.WaasAPISpec{} |
| 40 | + |
| 41 | + if val, ok := mapAPI["description"]; ok { |
| 42 | + api.Description = val.(string) |
| 43 | + } |
| 44 | + |
| 45 | + if val, ok := mapAPI["effect"]; ok { |
| 46 | + api.Effect = val.(string) |
| 47 | + } |
| 48 | + |
| 49 | + if schemaEndpoints, ok := mapAPI["endpoints"]; ok { |
| 50 | + iterEndpoints := schemaEndpoints.([]interface{}) |
| 51 | + for _, schemaEndpoint := range iterEndpoints { |
| 52 | + mapEndpoint := schemaEndpoint.(map[string]interface{}) |
| 53 | + endpoint := waas.WaasEndpoint{} |
| 54 | + |
| 55 | + if val, ok := mapEndpoint["base_path"]; ok { |
| 56 | + endpoint.BasePath = val.(string) |
| 57 | + } |
| 58 | + |
| 59 | + if val, ok := mapEndpoint["exposed_port"]; ok { |
| 60 | + endpoint.ExposedPort = val.(int) |
| 61 | + } |
| 62 | + |
| 63 | + if val, ok := mapEndpoint["grpc"]; ok { |
| 64 | + endpoint.GRPC = val.(bool) |
| 65 | + } |
| 66 | + |
| 67 | + if val, ok := mapEndpoint["host"]; ok { |
| 68 | + endpoint.Host = val.(string) |
| 69 | + } |
| 70 | + |
| 71 | + if val, ok := mapEndpoint["http2"]; ok { |
| 72 | + endpoint.HTTP2 = val.(bool) |
| 73 | + } |
| 74 | + |
| 75 | + if val, ok := mapEndpoint["internal_port"]; ok { |
| 76 | + endpoint.InternalPort = val.(int) |
| 77 | + } |
| 78 | + |
| 79 | + if val, ok := mapEndpoint["tls"]; ok { |
| 80 | + endpoint.TLS = val.(bool) |
| 81 | + } |
| 82 | + |
| 83 | + api.Endpoints = append(api.Endpoints, endpoint) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if val, ok := mapAPI["fallback_effect"]; ok { |
| 88 | + api.FallbackEffect = val.(string) |
| 89 | + } |
| 90 | + |
| 91 | + if schemaPaths, ok := mapAPI["paths"]; ok { |
| 92 | + iterPaths := schemaPaths.([]interface{}) |
| 93 | + for _, schemaPath := range iterPaths { |
| 94 | + mapPath := schemaPath.(map[string]interface{}) |
| 95 | + path := waas.WaasPath{} |
| 96 | + |
| 97 | + if schemaMethods, ok := mapPath["methods"]; ok { |
| 98 | + iterMethods := schemaMethods.([]interface{}) |
| 99 | + for _, schemaMethod := range iterMethods { |
| 100 | + mapMethod := schemaMethod.(map[string]interface{}) |
| 101 | + method := waas.WaasMethod{} |
| 102 | + |
| 103 | + if val, ok := mapMethod["method"]; ok { |
| 104 | + method.Method = val.(string) |
| 105 | + } |
| 106 | + |
| 107 | + if schemaParameters, ok := mapMethod["parameters"]; ok { |
| 108 | + iterParameters := schemaParameters.([]interface{}) |
| 109 | + for _, schemaParameter := range iterParameters { |
| 110 | + mapParameter := schemaParameter.(map[string]interface{}) |
| 111 | + parameter := waas.WaasParam{} |
| 112 | + |
| 113 | + if val, ok := mapParameter["allow_empty_value"]; ok { |
| 114 | + parameter.AllowEmptyValue = val.(bool) |
| 115 | + } |
| 116 | + |
| 117 | + if val, ok := mapParameter["array"]; ok { |
| 118 | + parameter.Array = val.(bool) |
| 119 | + } |
| 120 | + |
| 121 | + if val, ok := mapParameter["explode"]; ok { |
| 122 | + parameter.Explode = val.(bool) |
| 123 | + } |
| 124 | + |
| 125 | + if val, ok := mapParameter["location"]; ok { |
| 126 | + parameter.Location = val.(string) |
| 127 | + } |
| 128 | + |
| 129 | + if val, ok := mapParameter["max"]; ok { |
| 130 | + parameter.Max = val.(int) |
| 131 | + } |
| 132 | + |
| 133 | + if val, ok := mapParameter["min"]; ok { |
| 134 | + parameter.Min = val.(int) |
| 135 | + } |
| 136 | + |
| 137 | + if val, ok := mapParameter["name"]; ok { |
| 138 | + parameter.Name = val.(string) |
| 139 | + } |
| 140 | + |
| 141 | + if val, ok := mapParameter["required"]; ok { |
| 142 | + parameter.Required = val.(bool) |
| 143 | + } |
| 144 | + |
| 145 | + if val, ok := mapParameter["style"]; ok { |
| 146 | + parameter.Style = val.(string) |
| 147 | + } |
| 148 | + |
| 149 | + if val, ok := mapParameter["type"]; ok { |
| 150 | + parameter.Type = val.(string) |
| 151 | + } |
| 152 | + |
| 153 | + method.Parameters = append(method.Parameters, parameter) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + path.Methods = append(path.Methods, method) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + if val, ok := mapPath["path"]; ok { |
| 162 | + path.Path = val.(string) |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return policy, nil |
| 174 | + |
| 175 | +} |
0 commit comments