package pkg import ( "errors" "strconv" "strings" ) type WebAccelerationProfile struct { ProfileCommon Applilcations []string `json:"applications"` // none CacheAgingRate int `json:"cache_aging_rate"` // 9 CacheClientCacheControlMode string `json:"cache_client_cache_control_mode"` // all CacheInsertAgeHeader string `json:"cache_insert_age_header"` // enabled CacheMaxAge int `json:"cache_max_age"` // 3600 CacheMaxEntries int `json:"cache_max_entries"` // 10000 CacheObjectMaxSize int `json:"cache_object_max_size"` // 50000 CacheObjectMinSize int `json:"cache_object_min_size"` // 500 CacheSize string `json:"cache_size"` // 100mb CacheURIExclude []string `json:"cache_uri_exclude"` // none CacheURIInclude []string `json:"cache_uri_include"` // { .* } CacheURIIncludeOverride []string `json:"cache_uri_include_override"` // none CacheURIPinned []string `json:"cache_uri_pinned"` // none MetadataCacheMaxSize string `json:"metadata_cache_max_size"` // 25mb } func ParseWebAcceleration(raw string) (*WebAccelerationProfile, error) { p := new(WebAccelerationProfile) split := strings.Split(raw, "\n") if !strings.Contains(split[0], "ltm profile web-acceleration") { return nil, errors.New("not a web-acceleration profile") } p.ProfileCommon = parseCommon(raw) p.Applilcations = []string{} p.CacheURIExclude = []string{} p.CacheURIInclude = []string{} p.CacheURIIncludeOverride = []string{} p.CacheURIPinned = []string{} for _, line := range split { f := strings.Fields(line) switch strings.ToLower(f[0]) { case "applications": if f[1] != "none" { p.Applilcations = f[2 : len(f)-1] } case "cache-aging-rate": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.CacheAgingRate = i case "cache-client-cache-control-mode": p.CacheClientCacheControlMode = strings.Join(f[1:], " ") case "cache-insert-age-header": p.CacheInsertAgeHeader = strings.Join(f[1:], " ") case "cache-max-age": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.CacheMaxAge = i case "cache-max-entries": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.CacheMaxEntries = i case "cache-object-max-size": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.CacheObjectMaxSize = i case "cache-object-min-size": i, err := strconv.Atoi(f[1]) if err != nil { log.WithField("data", f[1]).Error(err.Error()) return nil, err } p.CacheObjectMinSize = i case "cache-size": p.CacheSize = strings.Join(f[1:], " ") case "cache-uri-exclude": if f[1] != "none" { p.CacheURIExclude = f[2 : len(f)-1] } case "cache-uri-include": if f[1] != "none" { p.CacheURIInclude = f[2 : len(f)-1] } case "cache-uri-include-override": if f[1] != "none" { p.CacheURIIncludeOverride = f[2 : len(f)-1] } case "cache-uri-pinned": if f[1] != "none" { p.CacheURIPinned = f[2 : len(f)-1] } case "metadata-cache-max-size": p.MetadataCacheMaxSize = strings.Join(f[1:], " ") } } return p, nil }